bubblesort help

closed account (4ET0pfjN)
Hi, I don't see why my bubblesort for [3,3,2] sorts to [2,2,2]!
I did not use any existing pseudocode, I just made up my own.

I'll explain in pseudocode
for each iteration of the list, do:
for each elem to compare still in cur unsorted list, do:
if cur position is at end of the list, then:
reduce unsorted list by 1
restart at front of unsorted list for next iteration
endif
else
if elem at cur position > elem at next position, then:
swap
endelse
endfor
endfor

and less pseudocode:
for each iteration of the list,
compare cur with adjacent elem to its right in cur unsorted list
if cur position not at end of list, then swap if needed


here is my code, I want to do bubblesort my way so please let me know what to fix with my code:
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
#include <iostream>

using namespace std;

int main()
{
int arraySize = 3;
int bubbleList_2[3] = {3, 3, 2};

cout<<"Original list:"<<endl;
//print original list
 for ( int i = 0; i < arraySize; i++ )
 {
   cout<<arraySize[i]<<endl;
 }

int curSize = arraySize;//we dec this index to largest elems
 //	"bubble" their way to the bottom (right side)
 for ( int i = 0; i < arraySize; i++ )//iterate list @ most (arraySize) times
 {
   cout<<"cur iteration:"<<i<<endl;
   cout<<"Cur size of unsorted list: "<<curSize<<endl<<endl;
   for ( int j = 0; j < curSize; j++ )//while still elm in unsorted list
   {
   //if @ end of unsorted list, reduce unsorted list, & iterate list again
    if ( j == curSize - 1 )
    {
     curSize-=1;
     break;
    }
			
   //else if cur postn (j) not last position, compare and swap if needed
   else if ( bubbleList_2[j] > bubbleList_2[j + 1] )
   {
    int temp = bubbleList_2[j];
    bubbleList_2[j] = bubbleList_2[j + 1];
    bubbleList[j + 1] = temp;
   }
			
   //else do nothing	
  }
 }
 
 //print sorted list
 for ( int i = 0; i < arraySize; i++ )
 {
   cout<<arraySize[i]<<endl;
 }
}//END MAIN
Last edited on
Line 37. Did you intend bubbleList[j + 1] or did you mean bubbleList_2[j + 1]?
closed account (4ET0pfjN)
Holy crap! Thanks! I can't believe such a dumb mistake! It's working fine!
Topic archived. No new replies allowed.