Skip to content

Instantly share code, notes, and snippets.

@ikasty
Last active August 29, 2015 14:06
Show Gist options
  • Save ikasty/18270b95dec5ae1de8d2 to your computer and use it in GitHub Desktop.
Save ikasty/18270b95dec5ae1de8d2 to your computer and use it in GitHub Desktop.
make sample input file for deterministic selection
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32
#define fileopen(fp, filename, type) fopen_s(&fp, filename, type)
#define fscanf fscanf_s
#else
#define fileopen(fp, filename, type) fp = fopen(filename, type)
#endif
#define LENGTH 1000 // > 30
#define TIPSIZE 31 // > 5
#define CASES 20
int array[LENGTH] = {14, 57, 24, 6, 37, 32, 2, 43, 30, 25, 23, 52, 12, 63, 3, 5, 44, 17, 34, 64, 10, 27, 48, 8, 19, 60, 21, 1, 55, 41, 29, 11, 58, 39};
int subarray[LENGTH];
FILE *ofp, *smp;
inline void swap(int *p, int *q) {
int temp;
temp = *p; *p = *q; *q = temp;
}
// selection sort
void sort(int *array, int start, int size) {
int i, j;
for (i = 0; i < size - 1; i++) {
int min = array[start + i];
int minp = i;
for (j = i + 1; j < size; j++) {
if (min > array[start + j]) {
min = array[start + j];
minp = j;
}
}
if (minp != i)
swap(&array[start + i], &array[start + minp]);
}
return ;
}
int main() {
int loop = 0;
int size, find, tip;
srand(time(0));
fileopen(ofp, "input.txt", "w");
fileopen(smp, "sample.txt", "w");
fprintf(ofp, "%d\n", CASES);
find = 7;
size = 34;
tip = 5;
while (++loop <= CASES) {
int i, j;
int hint;
if (loop > 1) {
size = rand() % (LENGTH - 30) + 30;
find = rand() % (size - 1) + 1;
tip = rand() % (TIPSIZE - 5) + 5;
//tip = (tip / 2) * 2 + 1; // make odd number
}
fprintf(ofp, "%d %d %d\n", find, size, tip);
for (i = 0; i < size; i++) {
if (loop > 1) array[i] = rand() % LENGTH + 1;
fprintf(ofp, "%d ", array[i]);
}
fprintf(ofp, "\n");
{
int set_count = (size - 1) / tip + 1;
int hint_count = set_count - 1;
for (i = 0; i < set_count - 1; i++) {
sort(array, i * tip, tip);
subarray[i] = array[i * tip + (tip / 2)];
}
sort(array, i * tip, (size - 1) % tip + 1);
if ((size - 1) % tip + 1 >= tip / 2 + 1) {
subarray[i] = array[i * tip + (tip / 2)];
hint_count++;
}
sort(subarray, 0, hint_count);
hint = subarray[hint_count / 2];
}
sort(array, 0, size);
fprintf(smp, "#%d %d %d\n", loop, array[find - 1], hint);
printf("#%d %d %d, k=%d, n=%d, a=%d\n", loop, array[find - 1], hint, find, size, tip);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment