мєяу´
| Konu: C-Örnekleri Salı Eyl. 22, 2009 10:50 pm | |
| Siralama algoritmalari, c dilinde örnekler... - Kod:
-
SelectionSort -
void SelectionSort(int *array, int size) { int x, y, z; int temp;
for(x = 0; x < size; x++) { z = x; temp = array[x]; for(y = x + 1; y < size; y++) { if(array[y] < temp) { temp = array[y]; z = y; } }
array[z] = array[x]; array[x] = temp; } }
InsertionSort
void InsertionSort(int *array, int size) { int x, y; int temp; for(x = 1; x < size; x++) { temp = array[x]; for(y = x - 1; y >= -1; y--) { if(temp < array[y] && y >= 0) { array[y + 1] = array[y]; } else { array[y + 1] = temp; break; } } }}BubbleSort void BubbleSort(int *array, int size) { int x, y; int temp; do { y = 0; for(x = 0; x < size - 1; x++) { if(array[x] > array[x + 1]) { temp = array[x]; array[x] = array[x + 1]; array[x + 1] = temp; y = 1; } } } while(y);}MergeSort void MergeSort(int *array, int size) { int start, stop, middle; int x, y, z, i; int *lp, *tp, *temp; lp = malloc(size * sizeof(int)); tp = array; for(i = 2; i < size << 1; i <<= 1) { for(start = 0; start < size; start += i) { stop = start + i - 1; middle = (stop + start) >> 1; if(stop >= size) stop = size - 1; if(middle >= size) middle = start; x = z = start; y = middle + 1; while(x <= middle && y <= stop) { if(tp[x] > tp[y]) lp[z++] = tp[y++]; else lp[z++] = tp[x++]; } while(x <= middle) lp[z++] = tp[x++]; while(y <= stop) lp[z++] = tp[y++]; } temp = tp; tp = lp; lp = temp; } if(tp != array) { for(x = 0; x < size; x++) { array[x] = tp[x]; } free(tp); } else { free(lp); }}QuickSort void QuickSort(int *array, int size) { int pivot, temp; int a, b; a = 0; b = size - 1; pivot = array[0]; for(; { while(array[a] < pivot) ++a; while(array[b] > pivot) --b; if(a > b) break; temp = array[a]; array[a] = array[b]; array[b] = temp; ++a; --b; } if(size > 1) { QuickSort(array, a); QuickSort(array + a, size - a); }}ShellSort void ShellSort(int *array, int size) { int off; int x, y; int temp; for(off = size >> 1; off; off >>= 1) { do { for(y = x = 0; x < size - off; x++) { if(array[x] > array[x + off]) { temp = array[x]; array[x] = array[x + off]; array[x + off] = temp; y = 1; } } } while(y); }} | |
|