Last active
April 1, 2016 08:08
-
-
Save Whosemario/2b121d8c5acf48b216372ff258c96b67 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void PushDown(int* numbers, int length, int index) { | |
| int value = numbers[index]; | |
| while(index*2+1 < length) { | |
| int t = index; | |
| if(index*2+1 < length && numbers[index*2+1] > value) { | |
| t = index * 2 + 1; | |
| } | |
| if(index*2+2 < length && numbers[index*2+2] > (t==index)? value : numbers[t]) { | |
| t = index * 2 + 2; | |
| } | |
| if(t == index) { | |
| numbers[index] = value; | |
| break; | |
| } else { | |
| numbers[index] = numbers[t]; | |
| index = t; | |
| } | |
| } | |
| if(index*2+1 >= length) numbers[index] = value; | |
| } | |
| void BuildHeap(int* numbers, int length) { | |
| for(int i = length/2-1; i >= 0; --i) { | |
| HeapDown(numbers, length, i); | |
| } | |
| } | |
| void HeapSort(int* numbers, int length) { | |
| if(numbers == NULL || length <= 1) return; | |
| BuildHeap(numbers, length); | |
| int tmp = numbers[0]; | |
| numbers[0] = numbers[length-1]; | |
| numbers[length-1] = tmp; | |
| length -= 1; | |
| while(length > 1) { | |
| PushDown(numbers, length, 0); | |
| tmp = numbers[0]; | |
| numbers[0] = numbers[length-1]; | |
| numbers[length-1] = tmp; | |
| length -= 1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void SelectSort(int* numbers, int length) { | |
| if(numbers == NULL || length <= 1) return; | |
| for(int i = 0; i < length; ++i) { | |
| int _min = numbers[i]; | |
| int _ind = i; | |
| for(int j = i+1; j < length; ++j) { | |
| if(numbers[j] < _min) { | |
| _min = numbers[j]; | |
| _ind = j; | |
| } | |
| } | |
| if(_ind != i) { | |
| numbers[_ind] = numbers[i]; | |
| numbers[i] = _min; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment