I've been trying to sort an array of 26 randomized integer inputs in such a way that you first sort the odd integers and then you sort the even integers in an ascending order. What I've tried so far is using the selection sort algorithm and applying an if statement.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int arr[26], i, min, j, p;
srand((unsigned)time(NULL));
for (i = 0; i < 26; i++) {
arr[i] = rand() % 100;
}
for (i = 0; i < 26; i++) {
printf("%2d ", i);
}
puts("");
for (i = 0; i < 26; i++) {
printf("%2d ", arr[i]);
}
for (i = 0; i < 26; i++) {
if (arr[i] % 2 != 0) {
min = i;
for (j = i + 1; j < 25; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
p = arr[i];
arr[i] = arr[min];
arr[min] = arr[i];
} else {
min = i;
for (j = i + 1; j < 25; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
p = arr[i];
arr[i] = arr[min];
arr[min] = arr[i];
}
}
puts("");
for (i = 0; i < 26; i++) {
printf("%2d ", arr[i]);
}
return 0;
}
That doesn't seem to be the correct way of doing it and I am a bit lost, since I'm a beginner. Could anyone provide any tips? Oh and apologies if my code looks like trash. Thank you for your answers
qsortas two different arrays.qsortcan be easily arranged that first it tests the "evenness" of the two items, and if the same, their value. Only oneqsortis needed.