is there a short way of doing this function

the program below is sorting out array element and place them in ascending order
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# include <iostream>
#include <string>
using namespace std;

//functio prototypes 
void printList(int [], const int);
void sortList(int [], const int);
void bubbleSort(int[], const int);



void main()
{
	const int SIZE_OF_ARRAY=5;
	int order_List[SIZE_OF_ARRAY]={21,13,17,5,3};
	// shows the unsorted index
	cout<<"Unsorted : ";
	printList(order_List, SIZE_OF_ARRAY);
	cout<<endl;
	//shows the sort and passes
	string passes[4]={"first","second","third","fourth"};
	
	for(int j=0;j<4;j++)
		{
			cout<<"This is the ";
			cout<< passes[j]<<" pass: ";
			bubbleSort(order_List,SIZE_OF_ARRAY);
			cout<<endl;
			    
		}
	
	//shows the sorted index
	cout<<"sorted Index : ";
	printList(order_List, SIZE_OF_ARRAY);
	cout<<endl;
	
}

//function PrintList Defintion
void printList(int order_List_2[],const int SIZE) 
{
	
	
	for(int count=0; count<SIZE;count++)
		{
			cout<<order_List_2[count]<<" ";
			
		}
	cout<<endl;
}

//function bubblesort definiton 
void bubbleSort(int sorting_list[], int SIZE1)
{
	//const int SIZE_OF_ARRAY=5;
	//int order_List[SIZE_OF_ARRAY]={21,13,17,5,3};
	sortList(sorting_list,SIZE1);
	printList(sorting_list,SIZE1);
}

void sortList(int sortlist[], const int SIZE2)
{
	int place_holder;

	if(sortlist[0]>=sortlist[1])
		{
			place_holder=sortlist[0];
			sortlist[0]=sortlist[1];
			sortlist[1]=place_holder;
		}
	 if(sortlist[1]>=sortlist[2])
		{
			place_holder=sortlist[1];
			sortlist[1]=sortlist[2];
			sortlist[2]=place_holder;
		}
	 if(sortlist[2]>=sortlist[3])
		{
			place_holder=sortlist[2];
			sortlist[2]=sortlist[3];
			sortlist[3]=place_holder;
		}
	 if(sortlist[3]>=sortlist[4])
		{
			place_holder=sortlist[3];
			sortlist[3]=sortlist[4];
			sortlist[4]=place_holder;
		}
			
			
}




i want to know if there is a short why to do this function? , in case i have to sort more next time.
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
void sortList(int sortlist[], const int SIZE2)
{
	int place_holder;

	if(sortlist[0]>=sortlist[1])
		{
			place_holder=sortlist[0];
			sortlist[0]=sortlist[1];
			sortlist[1]=place_holder;
		}
	 if(sortlist[1]>=sortlist[2])
		{
			place_holder=sortlist[1];
			sortlist[1]=sortlist[2];
			sortlist[2]=place_holder;
		}
	 if(sortlist[2]>=sortlist[3])
		{
			place_holder=sortlist[2];
			sortlist[2]=sortlist[3];
			sortlist[3]=place_holder;
		}
	 if(sortlist[3]>=sortlist[4])
		{
			place_holder=sortlist[3];
			sortlist[3]=sortlist[4];
			sortlist[4]=place_holder;
		}
			
			
}
Not only is that longer than it needs to be, it's useless unless the array has a size of 4. Just use a for loop in sortlist to loop through all of the elements in the array. You're already passing the size of the array into the function in fact but not using it.
Also, your function named "bubbleSort" should do the sorting, not farm out to another, less specifically named function to do its job, nor should it do things unrelated to sorting, like displaying stuff.

Hope this helps.
@ Duoas lol this is homework, i was given three prototypes and told to make a sort program from it
so that's why i have it set up like that.
@ quirky i tried it you way , and the result i got was that all changed at the same time , not the result i was looking for at all thanks though
Last edited on
That's fine and dandy, they are only trying to help and what they suggest is sound advice. You need to refactor your 'loop' code so that it uses a loop and not hardcoded values as quirkyusername suggested. And your bubblesort does not need to be printing. They are only trying to help and obviously help is due.

@clanmjc
yes obviously help is due, where on here did i say i did not appreciated the help; your logic makes no sense cause if i did not need help then i would not post on here. so your attempt at making me feel inferior has fail, and i have to print every pass that is why the bubblesort function is calling the print list function. you must be more of newbie than me to not realize that.
Look you shunned them off like they didn't know what they were talking about, I'm not trying to make you feel inferior, now that that is out of the way let me get to the point that Duoas stated. Your bubbleSort should do sorting, not printing, if you need to print each pass write another function that prints after the bubbleSort returns.

1
2
bubbleSort(order_List,SIZE_OF_ARRAY);
printList(sorting_list,SIZE1); 
As for the looping the way you have done it it is a hardcoded loop, but you have everything you need to make it a loop.

1
2
3
4
5
6
7
void sortList(int sortlist[], const int SIZE2)
{
   for(int i=0; i<SIZE2;++i)
   {
      //... do what you've hardcoded using i as the index, if you need i + 1 then use it.
   }
}


Last edited on
@clanmjc
first off thanks for the help, like i was trying to explain to Duoas is that this is homework Assignment iam full aware that the "bubble Sort" function should be doing the work, but that is not the way my teacher wants it to be setup, the code that you gave is exactly one that i tried before and it did not yield the results i need that what i was trying to to say to quirky,
If your code doesn't work, post it and will take it a look.
I guess that you are stepping out of bounds.
Sorry your teacher is being weird... and for the grief.

Draw yourself a bubble sort, and do it on paper. (You can use the four-element array you posted above for this.) Then think about how you have to make your index into the array work.

Also, keep in mind that every pass through the array puts the largest number in the array at the end, so you don't need to compare the just-sorted element at the end the next time through. In other words, the number of elements you need to sort in the array shrinks by one each time through, until you have but one element left.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.