Hi guys,
this is my first post here, and I'd love any feedback you guys can give me. I'm writing a basic program that bubble sorts its ints in an array to ascending order, then resorts some of those specific ints to the element of the position they represent.
What I mean by this is that if say the int 3 is the content of the zero element of my array after bubble sort, I want to move it to the second element so it would print out third. However, I want to keep the rest of the ints in otherwise ascending order.
Is there an easy way to compare the value of the contents of an array element to its position? I want to overwrite the original array and not have to use a second if possible.
What I have so far is below. It's the PuzSort function I'm having trouble with...
#include <stdio.h>
#include <stdlib.h>
void Sort( int ori_nums[], int limiting );
void PuzSort( int ori_nums[], int limiting );
int main( int argc, char *argv[] ) {
FILE *fin;
int *ori_nums;
int limiting, i;
fin = fopen( argv[1], "r" );
fscanf( fin, "%d", &limiting );
ori_nums = ( int* )malloc( limiting * sizeof( int ) );
for( i = 0; i < limiting; i++ ) fscanf( fin, "%d", &ori_nums[i] );
Sort( ori_nums, limiting );
PuzSort( ori_nums, limiting );
printf( "\n" );
for( i = 0; i < limiting; i++ ) printf( " %d ", ori_nums[i] );
printf( "\n" );
fclose( fin );
return 0;
}
void Sort( int ori_nums[], int limiting ) {
int i, j, temp;
for( i = 0; i < ( limiting - 1 ); i++ ) {
for( j = 0; j < ( limiting - 1 - i ); j++ ) {
if( ori_nums[j] > ori_nums[j+1] ) {
temp = ori_nums[j];
ori_nums[j] = ori_nums[j+1];
ori_nums[j+1] = temp;
}
}
}
return;
}
void PuzSort( int ori_nums[], int limiting ) {
int i, check, temp;
for( i = 0; i < (limiting - 1); i++ ) {
check = ( i + 1 );
if( ori_nums[i] <= limiting && ori_nums[i] <= (check) ) {
temp = ori_nums[check];
ori_nums[check] = ori_nums[i];
ori_nums[i] = temp;
}
}
return;
}