Insertion sort

I was working on the last question of a basic program and I'm having trouble implementing an insertion sort for the following task:

★★★★ Modify the program so that it outputs a list in order of number of pancakes eaten of all 10 people.
i.e.
Person 4: ate 10 pancakes
Person 3: ate 7 pancakes
Person 8: ate 4 pancakes
...
Person 5: ate 0 pancakes

I have seen more efficient ways to accomplish this but I can't seem to figure out how to output what the task asks for with an insertion sort.


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 namespace std;

void display_array(int x[], int size);
void min_max(int x[], int size);
void insertion_sort(int arr[], int size);

int main()
{
	int i, size=10, pers[10];

	for (i = 0; i < size; i++)
	{
		cout << "How many pancakes did person " << (i + 1) << " eat?" << endl;
		cin >> pers[i];
	}
	
	min_max(pers,size);
	insertion_sort(pers, size);

	system("pause");
	return 0;
}

void display_array(int x[], int size)
{
	for (int i = 0; i < size; i++)
	{
		cout << x[i] << " ";
	}
}

void min_max(int x[], int size)
{
	int maxPers=1, minPers=1;
	int min = x[0];
	int max = x[0];
	for (int i = 0; i < size; i++)
	{
		if (x[i]>max)
		{
			max = x[i];
			maxPers = i + 1;
		}
		if (x[i] < min)
		{
			min = x[i];
			minPers = i + 1;
		}
	}


	cout << "Person " << maxPers << " ate the most pancakes! " << ": " << max << endl;
	cout << "Person " << minPers << " ate the least amount of pancakes" << ": " << min << endl;
}

void insertion_sort(int arr[], int size)
{
	int j, temp,i=0;
	for (i = 0; i < size;i++)
	{
		j = i;
		while (j>0 && arr[j - 1] > arr[j])
		{
			temp = arr[j];
			arr[j] = arr[j - 1];
			arr[j - 1] = temp;
			j--;
		}
			
	}
	display_array(arr, size);
}
Topic archived. No new replies allowed.