infinite loop - Language C - QuickSort -
i trying code quick sort program in c, , recursive function in infinite loop.
the problem is:
you have been given array of size n.this array contains integers ranging 1 10^9. need sort contents of array value , print contents of it.
input format:
the first line contains single integers n denoting size of array. next line contains n space separated integers denoting contents of array.
output format:
print n space separated integers, i.e. final sorted array.
constraints:
1≤n≤10^6
1≤a[i]≤10^9
void fill_vet(unsigned int a[], unsigned int n){ int i; for(i=0; i<n; i++) scanf("%u", &a[i]); } void print_vet(unsigned int a[], unsigned int n){ int i; for(i=0; i<n; i++) printf("%u ", a[i]); } int partition(unsigned int a[], int start, int end){ int i, j; unsigned int pivot, n_swap; = start + 1; pivot = a[start]; for(j = start+1; j<=end; j++){ if(a[j] < pivot){ n_swap = a[j]; a[j] = a[i]; a[i] = n_swap; i++; } } n_swap = a[i-1]; a[i-1] = a[start]; a[start] = n_swap; return i-1; } void sort_array(unsigned int a[], int start, int end){ while(start < end){ int pos_piv = partition(a, start, end); sort_array(a, start, pos_piv-1); sort_array(a, pos_piv+1, end); } } int main() { unsigned int n, a[100000]; scanf("%u", &n); fill_vet(a, n); sort_array(a, 0, n-1); print_vet(a, n); return 0; }
can tell me error pls!
you using recursion don't need loop:
void sort_array(unsigned int a[], int start, int end){ if(start < end){ int pos_piv = partition(a, start, end); sort_array(a, start, pos_piv-1); sort_array(a, pos_piv+1, end); } }
Comments
Post a Comment