Sorting Arrays

arranging orders of elements

Page 1 of 1

1 Replies - 329 Views - Last Post: 12 March 2009 - 04:02 AM Rate Topic: -----

#1 lynn16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-December 08

Sorting Arrays

Posted 12 March 2009 - 03:42 AM

{ int elements[N] = {3,-4,12,9,-1}, x[N] = {3,-4,12,9,-1}, i, j, temp;
  for (i=0;i<=N;i++)
  { for (j=i+1;j<N;j++)
	{ if (elements[i]<elements[j])
	  { temp = x[i];
		x[i] = x[j];
		x[j] = temp; 
	   }
	  }
   }



the values of elements [N] will still be the same after the program runs right?

^^

This post has been edited by lynn16: 12 March 2009 - 03:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Arrays

#2 tjust80  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 73
  • Joined: 01-December 08

Re: Sorting Arrays

Posted 12 March 2009 - 04:02 AM

try this

#define N 10
main()
{ 
	int elements[N] = {3,-4,12,100,89,2,14,-150,9,-1}, i, j, temp;
  
	for (i=0;i<N;i++) 
	{ 
		for (j=i+1;j<N;j++)
		{
			if (elements[i]<elements[j])
			{
				temp = elements[i];
				elements[i] = elements[j];
				elements[j] = temp;
			}
		}
	}
	for (i=0;i<N;i++) 
	{
		cout << elements[i] << endl;
	}
}


This post has been edited by tjust80: 12 March 2009 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1