void genIntArr(int a[], int n); void swap(int *a, int *b); void printIntArr(int a[], const int size);
/* * in java, Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER); */ void insenBubble(char *str);
void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }
//================================ // bubble sort int array // O(n^2),stable //================================ void bubbleSort(int arr[], const int size) { int i,j,flag; int temp = 0;
for(i=0; i { flag = 1; for(j=0; j { if(arr[j]>arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; flag = 0; } } if(flag) break; } }
/* Case Insensitive Char Compare * @return 1 for sa>sb, or 0. */ int insenbigger(char sa, char sb) { // toupper if(sa>='a' && sa<='z') sa -= ('a'-'A'); if(sb>='a' && sb<='z') sb -= ('a'-'A'); //compare if( (sa - sb) > 0 ) return 1; else return 0; }
/* * in java, Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER); */ void insenBubble(char *str) { int i,j,flag; if(NULL == str) return; int len = strlen(str); char c;
for(i=0; i { flag = 1; for(j=0; j { if(insenbigger(str[j],str[j+1])) { c = str[j]; str[j] = str[j+1]; str[j+1] = c; flag = 0; } } if(flag) break; } }
//======================================== // selection sort. O(n^2) Not stable // is better than bubble when n is small //======================================== void selectSort(int a[], int n) { int i,j,t,min; for(i=0; i { min = i; //find the minimum for(j=i+1; j { // (n-1)+(n-2)+...+1 = n*(n-1)/2 compare if(a[j] min = j; } swap(&a[min], &a[i]); } }