arrays - descending order is not working properly c programming -



arrays - descending order is not working properly c programming -

the ascending order doing great job. when come in number different one, should phone call descending order, think defined. yet have random sorting..

#include<stdio.h> void sort(int b[], int size, int (*upordown)(int a, int b)); void swap(int *elt1, int *elt2); int ascending(int a, int b); int descending(int a, int b); main() { int size =8; int order,a[size],i; fprintf(stdout, "please come in sequence of numbers wish sort:\n"); for(i =0; i<size; i++) { scanf("%d", &a[i]); } printf("\n please come in 1 ascending order or 2 descending order"); scanf("%d", &order); if(order == 1) { sort(a, size, ascending); } else { sort(a, size, descending); } printf("here new re arranged array: \n"); for(i =0; i<size; i++) { printf("%d\t", a[i]); } printf("\n"); homecoming 0; } void sort(int b[], int size, int (*upordown)(int a, int b)) { int pass, j; for(pass =0; pass< size;pass++) { for(j=0; j<size; j++) { if((*upordown)(b[j], b[j+1])) { swap(&b[j], &b[j+1]); } } } } void swap(int *elt1, int *elt2) { int hold; if(*elt1 > *elt2) { hold = *elt1; *elt1 = *elt2; *elt2 = hold; } } int ascending(int a, int b) { homecoming b<a; } int descending(int a, int b) { homecoming b>a; }

that's because swap function incorrect. should swap passed parameters:

void swap(int *elt1, int *elt2) { int hold; hold = *elt1; *elt1 = *elt2; *elt2 = hold; }

it should not contain comparison.

c arrays sorting

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -