I've worked for two days on this algorithm but I'm stumped.
It's not printing out what the instructor states.
Where am I going wrong?
Thanks in advance,
Mike
/*Arrange the following single dimension array in column major order. (Yes, the entire array, not just each column separately)
4 5 6
4 5 6
7 8 9
10 11 12
7 8 9
RULES: DO NOT use printf in some way to make it print in the correct order.
Instead, Move the items in the linear array to make this so.
if printed linearly it should print:
4,6,9,4,7,9,5,7,10,5,8,11,6,8,12.
*/
Code:
#include<stdio.h>
int main(){
int array[15]={4,5,6,4,5,6,7,8,9,10,11,12,7,8,9};
int i,top,temp;
for(top=0; top<=14; top=top+1){
for(i=top+1; i<=14; i++){
if(array[top]>array[i]){
temp=array[top];
array[top]=array[i];
array[i]=temp;
break;
}
if(array[top]==array[i] && (top+1)<14 && (top!=i && (top+1)!=i)){
temp=array[top+1];
array[top+1]=array[i];
array[i]=temp;
break;
}
}
}
for(i=0; i<15; i++)
printf("%d,", array[i]);
return 1;
}