please help me in finding what is wrong with my array... its not sorting
thanks, thanks.

int main(){
	int i,j,x,y;

	int unsort[10] = { 34, 76, 12, 1, 56, 23, 65, 9089, 45, 63 };
	//int sort[10];
	
	
	for (i = 0; i < 10; i++){
		cout<<unsort[i]<<" ";

	}

	for (j = 0; j < unsort[10-i-1]; j++){
			
		if (unsort[j] > unsort[j+1]){
				
				x = unsort[j];
				unsort[j] = unsort[j+1];
				unsort[j+1] = x;
			}
	}
	

	//SORTED ARRAYS 
	for (y = 0; y < unsort[10]; y++)
		cout <<unsort[y];
	
	return 0;
}

What is the end condition in your for statement? There, your i is 10, you iterate up to the value of some element of your array, which is bigger than the array size. You go beyond the limit of the array, which may cause any kind of unexpected behavior. But, to understand better what happens, get used to add debug statements in your code, these are most often printf functions, printing the value of some variable, sometimes you may add some condition, or even make them more complicated. Distinguish the debug statements somehow, the advanced way is to put them between something like #ifdef DEBUG and #endif, but i mostly just used to add a comment /***/, after them... Then you'll see exactly what happens.

you are not supposed to use the value of an array element in the for loop condition, but use the max number of elements in the loop, something like this:

for (j = 0; j < 10; j++){

dear austinslik,

please use code below. for sorting an array you could use one of sorting algorithms as below. here I use quick sort algorithm. you also could write bubble sort, insertion sort,.......

int iQuickSort( int aiBuffer[], int iLeft, int iRigth)    
{  
        int iRef, iTemp;
        int iLeftIndex, iRigthIndex;    
    
        iLeftIndex=iLeft;    
        iRigthIndex=iRigth;          
    
        iRef = aiBuffer[(iLeft+iRigth)/2];
        do 
        {
        while( aiBuffer[iLeftIndex]<iRef && iLeftIndex<iRigth)
            iLeftIndex++;
               while( iRef<aiBuffer[iRigthIndex] && iRigthIndex>iLeft )
                   iRigthIndex--;
               
               if(iLeftIndex<=iRigthIndex)
               {  
                  iTemp = aiBuffer[iLeftIndex];
                  aiBuffer[iLeftIndex] = aiBuffer[iRigthIndex];
                  aiBuffer[iRigthIndex] = iTemp;
                  iLeftIndex++;
                   iRigthIndex--;           
               }
    }
    while( iLeftIndex<=iRigthIndex );
  
      if( iLeft<iRigthIndex) iQuickSort( aiBuffer, iLeft, iRigthIndex );
      if( iLeftIndex<iRigth ) iQuickSort( aiBuffer, iLeftIndex, iRigth );

      return 0;         
}

int main(int argc, char *argv[])
{
    int i,j,x,y;

    int unsort[10] = { 34, 76, 12, 1, 56, 23, 65, 9089, 45, 63 };
    
    
    for (i = 0; i < 10; i++)
        cout<<unsort[i]<<" ";

     iQuickSort( unsort, 0, 9 );
 
        cout << "\n";    

    //SORTED ARRAYS 
    for (i = 0; i < 10; i++)
        cout<<unsort[i]<<" ";
        
        cout << "\n";    
        
        system("PAUSE");
        return EXIT_SUCCESS;
}

please help me in finding what is wrong with my array... its not sorting
thanks, thanks.

First, what Ancient Dragon said is correct.
Second, your sort needs two loops, not one. Look at the description or algorithm of the sort again. Your code should be moving the highest value into place (once you fix the loop parameters). Then add that second loop.

cout<<unsort[i]<<" ";
cout <<unsort[y];

These are not valid C programming statements.

cout<<unsort[i]<<" ";
cout <<unsort[y];

These are not valid C programming statements.

you are right -- I'll move it over to the c++ board.

for (i = 0; i < 10; i++){ cout<<unsort<<" "; } for (j = 0; j < unsort[10-i-1]; j++){ if (unsort[j] > unsort[j+1]){ x = unsort[j]; unsort[j] = unsort[j+1]; unsort[j+1] = x; } }

In your second for loop, when you use i, isn't that already supposed to be 10 from previous for loop. Hence giving you the starting loop of

for(j = 0; j < -1; j++){
}...

The simplest bubble sort...

#include <stdio.h>

int main ()
{
  int t, i, j, arr [] =
    {3, 6, 1, 2, 3, 8, 4, 1, 7, 2, 0};

  for (i = 0; arr [i]; i++);
  for (; i > 1; i--)
    for (j = 1; j < i; j++)
      if ((t = arr [j - 1]) > arr [j]) {
        arr [j - 1] = arr [j];
        arr [j] = t;
      }
  for (i = 0; arr [i]; i++)
    printf ("%d ", arr [i]);
  printf ("\n");
  return 0;
}
main()
{
	int a[10],i,j,temp=0;
	clrscr();

	// enterinr value to array	

	for(i=0;i<10;i++)
	{
	printf("\n enter number=");
            scanf("%d",&a[i]);

	}

	// sorting array

	for(i=0;i<10;i++)
	{
		for(j=i+1;j<10;j++)
		{
			if(a[i]>a[j])
			{
			temp=a[i];
			a[i]=a[j];
			a[j]=temp;
			}
		}
	}
	// printing array
	for(i=0;i<10;i++)
	{
	printf("\n%d",a[i]);
	}
	getch();
}

I HOPE THIS IS HELPFUL TO YOU

THANKS
Meghs

sorry this feedback came late...
i used double loop and it worked.

Thanx everybody for the help.

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