Quicksort and file rearrange
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// Capture the pipe of array contents, first number is the size
|
||||
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 = 0; j < s; j++) {
|
||||
int key = array[j];
|
||||
int i = j - 1;
|
||||
while (i >= 0 && array[i] > key) {
|
||||
array[i + 1] = array[i];
|
||||
i -= 1;
|
||||
}
|
||||
array[i + 1] = key;
|
||||
}
|
||||
|
||||
free(array);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user