number Sorting algorithm

Damn guys, so ive been trying to work on this for quite some time now and just feel a bit stupid.

I want the user to input 10 numbers, and my program to sort all 10 from greatest to least. for now all i can come up with is a repetition of the greatest number:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;


int main()
{

    int pancakes[10];

    for(int i=0; i<10; i++)
    {
        cin>>pancakes[i];
    }

    cout<<"\n--------------\n\n";

    for(int i=0; i<10; i++)
    {
        cout<<i+1<<") "<<pancakes[i]<<endl;
    }

    int ordered[10];

    for (int i=0; i<10; i++)
        ordered[i]=pancakes[i];

    for(int i=0; i<10; i++)
    {
        for(int a=0; a<10; a++)
        {
            if(ordered[i]>=pancakes[a]) ordered[i]=ordered[i];

            else if(pancakes[a]>=ordered[i]) ordered[i]=pancakes[a];
        }
    }

    cout<<"\n---Ordered---\n\n";

    for(int i=0; i<10; i++)
    {
        cout<<i+1<<") "<<ordered[i]<<endl;
    }

    return 0;
}


this works only to find the greatest number and just finds it over and over ten times... im stuck. if anyone could give me a hint?
http://en.wikipedia.org/wiki/Sorting_algorithm

PS: Why do you include cstdlib and vector if you aren't using them anyways?
at first i was going to use vectors, and also i removed some system("blah") so i wouldnt get yelled at.
so before i delve into the wiki sorting algorithms arent easy? I would think it would be some simple trick in the nested loop since they ask for it on the http://www.cplusplus.com/forum/articles/12974/ pancake glutton sample problem
Let me make your life a little easier - see http://www.cplusplus.com/reference/algorithm/sort/

PS - This also works on C arrays, not just vectors ;)
Last edited on
thats an already pre-written sorting algorithm? Yeah thanks and i probably will use that but i would like to learn how it works also
Well yeah, I already gave you a link with sorting algorithms right? Bubble and Selection sort are pretty easy to implement.
some of those algorithms are stuff i could have come up on my own, like the one that sais it "holds" the highest number it finds and so on till it gets to the lowest one. but that whole "holding" part is what i dunno how to do...
Uh... variables?
no i mean like how do you make it so that your program doesnt do any operations to that variable that youre "holding" But i think im getting off the wrong track about it ima give it one more try before reading those links. thanks for all the help guys.
i know its late but
This is a code from my professor, he explained it pretty good.
and i almost use this in my every sorting program.
and for u all u need to do is when the array is sorted... just reverse it
see if it helps
sincerly,
java programmer lol

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
////////////////////////////////////////////////////////////
//
// Bubble sort code for an array "a" of integers, where "n"
// is the number of data items (integers) currently in the
// array.
//
// Assumption: n > 1
//////////////////////////////////////////////////////////////

//
// Additional variables used:
//
//      bool exchange_made;
//      int num_items_to_visit;
//      int last_marker;
//      int i;
//      int temp;
//

      // set the number of values (elmts) to visit on the first pass.
      // Note that it is actually one less than the total #
      // of data values since the comparison of the next-to-last
      // and last values occurs while visiting the next-to-last.

      num_items_to_visit = n - 1;

      do
      {

      // no exchange has occurred yet on the current pass.
      // (We haven't scanned any values yet!)
      // Indicate this by lowering the "exchange_made" flag.

	 exchange_made = false;

      // the index of the last item to visit on this pass is
      // one less than the # to visit.

         last_marker = num_items_to_visit - 1;

      // scan the values

	 for(i = 0; i <= last_marker; i++)
         
	    if(a[i] > a[i+1])
	    {

		// swap them

		temp = a[i];
		a[i] = a[i+1];
		a[i+1] = temp;

		// indicate that a swap has been made

		exchange_made = true;

            }

         // number of items to visit is now one less.

	 num_items_to_visit--;

      } while( exchange_made and (num_items_to_visit > 0) );

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

int main()
{
	int aray[10]={12,8,9,7,5,31,6,2,1,14};

	for(int i=0; i<10; i++)
	{
		cout<<i+1<<") "<<aray[i]<<endl;
	}

	cin.ignore();

/////////////////Bubble Sort/////////////////////
	for (int i=0; i<10; i++)
	{
		for (int x=0; x<9; x++)
		{
			if(aray[x]>aray[x+1])
			{

				int temp=aray[x+1];

				aray[x+1]=aray[x];

				aray[x]=temp;

			}
		}
	}
/////////////////END:Bubble Sort///////////////

	cout<<"\n---Sorted---\n\n";

	for(int i=0; i<10; i++)
	{
		cout<<i+1<<") "<<aray[i]<<endl;
	}

	cin.ignore();

}


Hope im not rezzing a pointless thread but i got it to work. using bubble sort method. Thanks for all the help guys

EDIT: Oh karanhs beat me to it :(
Last edited on
@valad61 : your code have problem ! and i was edit it again !


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>

using namespace std;

int main()
{
	int aray[10]={12,8,9,7,5,31,6,2,1,14};

	for(int i=0; i<10; i++)
	{
		cout<<i+1<<") "<<aray[i]<<endl;
	}

//	cin.ignore();

/////////////////Bubble Sort/////////////////////
	for (int i =9 ;  i >= 0 ; i--)
	{
		for (int  x = 0 ; x< 10; x++)
		{
			if(aray[x]>aray[x+1])
			{

				int temp=aray[x+1];

				aray[x+1]=aray[x];

				aray[x]=temp;

			}
		}
	}
/////////////////END:Bubble Sort///////////////

	cout<<"\n---Sorted---\n\n";

	for(int i=0; i<10; i++)
	{
		cout<<i+1<<") "<<aray[i]<<endl;
	}

	cin.ignore();
	system ("pause");
	return 0;

}
Topic archived. No new replies allowed.