Last active
January 9, 2017 06:30
-
-
Save CykuTW/b7b061cbac209aba066748c7fc09b416 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| int mySort(int *numbers, size_t size) { | |
| //sort by increment | |
| int *data = (int*) malloc(size*sizeof(int)); | |
| int *weights = (int*) malloc(size*sizeof(int)); | |
| int i, j, k; | |
| memset(weights, 0, size*sizeof(int)); | |
| for (i=0; i<size; i++) { | |
| data[i] = numbers[i]; | |
| for (j=0; j<size; j++) | |
| if (numbers[i] < numbers[j]) //count how many numbers are biger than numbers[i]. | |
| weights[i]++; | |
| } | |
| //the most weight is always small than size | |
| for (i=size-1, k=0; i>=0; i--) | |
| for (j=0; j<size; j++) | |
| if (weights[j] == i) | |
| numbers[k++] = data[j]; | |
| } | |
| int main(void) { | |
| int numbers[] = {12, 6, 9, 4, 7, 2, 15}; | |
| mySort(numbers, 7); | |
| int i; | |
| for (i=0; i<7; i++) | |
| printf("%d ", numbers[i]); | |
| //2, 4, 6, 7, 9, 12, 15 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment