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;

}

Would it be correct to move all the values higher than index 2, in your example, and then assign the 3 to array[2]? for example:

int array[] = {3, 9, 7, 5, 11, 4, 8};

After sorting:
{3,4,5,7,8,9,11}

After placing the 3 in element 2 of the array:
{4,5,3,7,8,9,11} <=== Final placement in array

Would it be correct to move all the values higher than index 2, in your example, and then assign the 3 to array[2]? for example:

int array[] = {3, 9, 7, 5, 11, 4, 8};

After sorting:
{3,4,5,7,8,9,11}

After placing the 3 in element 2 of the array:
{4,5,3,7,8,9,11} <=== Final placement in array

Not quite. I don't think I really made this clear, but the shifting would be done on multiple elements in the same array. The 'limiting' variable is an int that tells both how many elements are in the array, and which numbers should be shifted in the second sorting. If the int was 5, there would be five elements, but any elements that are less than 5 should be in their repective positions -
int array[] = {2, 3, 5, 12, 31};

would become

{12, 2, 3, 31, 5}

because numbers ascending takes lesser precedence than numbers in their positions (2 in second slot, three in third, etc.).

This article has been dead for over six months. Start a new discussion instead.