Array sorting feedback appreciated :)

I was doing the Pancake Glutton excercise and just started trying to do a bit more with it. I used a 2D array and wanted to sort the numbers from lowest to highest. There were a couple of things:

Firstly, the program kept falling off the end of the array. I put my own solution in but have no idea if it is a good way to do it, I'm assuming it was a simple mistake I made to do with the zero first element thing.

Secondly, I noticed that with each "pass" the code made to check and swap values, the highest number is always sent to the bottom position(which is what I wanted) but that it needlessly checks the highest values with each subsequent pass. I have managed to get it to check one less element each time, but would like to know how it looks and if it could be better in general.

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
  #include<iostream>
 

  using std::cout;
  using std::cin;
  using std::endl;


  int main()
  {
        const int ROWS = 10;
 	const int COLUMNS = 2;

	int person[ROWS][COLUMNS] =
	{
		{ 1,100 },
		{ 2,20 },
		{ 3,80 },
		{ 4,10 },
		{ 5,50 },
		{ 6,150 },
		{ 7,30 },
		{ 8,60 },
		{ 9,40 },
		{ 10,1 } 
	};
           for (int i = 0; i < ROWS; ++i)
	{
	cout << "Person " << person[i][0] << " ate: " << person[i][1] << " pancakes";
		{
			cout << endl;
		}
	}

	// Sort lowest to highest

	int loop = 1;
	int count;
	int limit = ROWS;

	for (int j = ROWS - 1; j > 0; --j, loop++, limit--)
	{
		cout << "Loop " << loop << endl;
		cout << "Loop limit: " << limit << endl;
		count = 0;

		for (int i = 0; i < ROWS; ++i, ++count )
		{
		  if (loop + count == ROWS + 1)// skips uneccessary checks
		  {
			break;
		  }
		  if (person[i][1] > person[i + 1][1] && i != ROWS - 1)
 /* added the && condition to stop falling off array, but don't know why it worked*/
		  {
		  	int temp = person[i][1];
			person[i][1] = person[i + 1][1];
			person[i + 1][1] = temp;
  		  }
			//cout << person[i][0] << "	" << person[i][1]<<endl;
		}
		cout << "Number of inner loops: " << count << "\n" << endl;
	}

	for (int i = 0; i < ROWS; ++i)
	{
		cout << person[i][0] << "	" << person[i][1] << endl;
	}
    int pause;
	cin >> pause;
	
    return 0;
}
Last edited on
I used a 2D array and wanted to sort the numbers from lowest to highest.

Why a 2d array, a single dimensional array should be easier.

Secondly,

What type of sort are you trying to implement? Ie: bubble sort, insertion sort, shell sort, etc. I'd recommend that you try to implement a single bubble sort.

https://www.geeksforgeeks.org/bubble-sort/

Or just use std::sort. https://en.cppreference.com/w/cpp/algorithm/sort

I used a 2d array to add another complication for me to work around. Yes a single dimension would be easier, but why would I want that?

What type of sort? Well, the type that arranges numbers from lowest to highest? I’m not sure I understand that question. I did not look up “how to” examples because, again, it defeats the objective. Eventually I would like to write a simple AI program, and for that I’m almost certainly going to need to create my own algorithms. That’s the same reason I didn’t look at using things like std::sort.

The program does what it’s supposed to, I was really looking for some feedback on what I’d written.

Edit: typo
Last edited on
Topic archived. No new replies allowed.