Quicksort and file rearrange
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
int s;
|
||||
int res = scanf("%d", &s);
|
||||
if (res != 1) {
|
||||
fprintf(stderr, "Error reading size\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int *array = malloc(sizeof(int) * s);
|
||||
for (int i = 0; i < s; i++) {
|
||||
res = scanf("%d", &array[i]);
|
||||
if (res != 1) {
|
||||
fprintf(stderr, "Error reading array element\n");
|
||||
free(array);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = s; j > 1; j--) {
|
||||
int max = j;
|
||||
for (int i = j - 1; i >= 0; i--) {
|
||||
if (array[i] > array[max]) {
|
||||
max = i;
|
||||
};
|
||||
};
|
||||
int temp = array[j];
|
||||
array[j] = array[max];
|
||||
array[max] = temp;
|
||||
}
|
||||
|
||||
free(array);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user