Hello, have a problem.
Basically, I have an large array. I want to take the 10 smallest numbers out of a large list (200 and can vary). Doing so by taking the numbers that fit this criteria and placing it into a smaller, and sorted, array. Using just a bubble sort for the 10 element array.
First though, just hardcoding an array to try and get it to work.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 10
#define X 15
/* This program shows you how to a psuedo insertion sort.
*/
int main(void)
{
float *array, *data, value, dump;
int index_of_min;
int i, j, z;
int flag;
array = malloc(N * sizeof(float));
data = malloc(X * sizeof(float));
/* Fill in data */
for(i = 0; i < X; i++){
if(i == 0)
data[i] = 23.0;
if(i < 5 && i != 0)
data[i] = i * 3.5;
if(i >= 5)
data[i] = i + 2;
}
for(i = 0; i < N; i++){
array[i] = 99999.0;
}
/* Print data out first */
printf("The data set is:\n");
for(i = 0; i<X; i++){
printf("data[%d] = %f\n", i, data[i]);
}
printf("*** ----------------------------------------------- ***\n");
printf("The ARRAY of smallest ten is:\n");
for(i = 0; i<N; i++){
printf("array[%d] = %f\n", i, array[i]);
}
printf("*** ----------------------------------------------- ***\n");
/* Loop through observations. */
for(z = 0; z < X; z++){
flag = 0;
if(data[z] < array[9]){
array[9] = data[z];
dump = array[9];
flag = 1;
}
/* Sort the limited array of 10 with the new element */
if(flag == 1){
for(i = 0; i < N; i++){
for(j = 0; j < N-1; j++){
if(array[j]>array[j+1]){
value = array[j+1];
array[j+1] = array[j];
array[j] = value;
}
}
}
/*
for(i = (N - 1); i >= 0; i--){
for(j = 1; j <= i; j++){
if(array[j-1] > array[j]){
value = array[j-1];
array[j-1] = array[j];
array[j] = value;
}
}
}
*/
}
/* End sort */
}
/* *************************************************************** */
/* Print out the 10 smallest */
printf("The smallest elements of the data set sorted is:\n");
for(i = 0; i<N; i++){
printf("array[%d] = %f\n", i, data[i]);
}
free(data);
free(array);
return 0;
}
As you can see, I have also done the bubble sort on a smaller list just to see if that algorithm works -- it does... It seems my method is write and I tried doing by hand and to me -- it should work. Any ideas!
Also, I know the bubble sort is not looked upon highly. I will probably change to an insertion method - but first want to get it done with that alogirthm.
I get output like:
Code:
The data set is:
data[0] = 23.000000
data[1] = 3.500000
data[2] = 7.000000
data[3] = 10.500000
data[4] = 14.000000
data[5] = 7.000000
data[6] = 8.000000
data[7] = 9.000000
data[8] = 10.000000
data[9] = 11.000000
data[10] = 12.000000
data[11] = 13.000000
data[12] = 14.000000
data[13] = 15.000000
data[14] = 16.000000
*** ----------------------------------------------- ***
The ARRAY of smallest ten is:
array[0] = 99999.000000
array[1] = 99999.000000
array[2] = 99999.000000
array[3] = 99999.000000
array[4] = 99999.000000
array[5] = 99999.000000
array[6] = 99999.000000
array[7] = 99999.000000
array[8] = 99999.000000
array[9] = 99999.000000
*** ----------------------------------------------- ***
The smallest elements of the data set sorted is:
array[0] = 23.000000
array[1] = 3.500000
array[2] = 7.000000
array[3] = 10.500000
array[4] = 14.000000
array[5] = 7.000000
array[6] = 8.000000
array[7] = 9.000000
array[8] = 10.000000
array[9] = 11.000000