In: Computer Science
The task is to sort structs in array. The struct is
struct coor{ int x; int y; int z; };
Create an array with 100 coordinates, which are randomly initiated with x between 0 and 30 and y between 5 and 60, and z between 10 and 90. Then modify the Quick Sort algorithm (QuickSort.cpp) to sort coordinated. Comparison of the coordinates should be carried on first x, then y, and z last. It means that if two points has same x values, then you should compare y values of them. If y values are same too, then compared z values.
Print sorted coordinated.
#include<iostream>
#include<cstdlib>
using namespace std;
struct coor
{
int x;
int y;
int z;
};
void accept(struct coor list[], int s);
void display(struct coor list[], int s);
int partition (struct coor list[], int low, int high);
void quickSort(struct coor list[], int low, int high);
int main()
{
struct coor data[100];
int n;
printf("Number of records you want to enter? : ");
scanf("%d", &n);
accept(data, n);
printf("\nBefore sorting:");
display(data, n);
// bsortDesc(data, n);
quickSort(data,0,n-1);
printf("\nAfter sorting:");
display(data, n);
return 0;
}
void accept(struct coor list[10], int s)
{
int i;
for (i = 0; i < s; i++)
{
list[i].x=rand()%29+1;
list[i].y=rand()%55 + 5;
list[i].z=rand()%80 +10;
}
}
void display(struct coor list[], int s)
{
int i;
printf("\n\nX\tY\tZ\n");
for (i = 0; i < s; i++)
{
printf("%d\t%d\t%d\n", list[i].x, list[i].y, list[i].z);
}
}
int partition (struct coor list[], int low, int high)
{
struct coor pivot = list[high],temp; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high- 1; j++)
{
// If current element is smaller than or
// equal to pivot
if (list[j].x < pivot.x)
{
i++;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
if(list[j].x==pivot.x && list[j].y<pivot.y)
{
i++;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
if(list[j].x==pivot.x && list[j].y==pivot.y && list[j].z<list[j].z)
{
i++;
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
temp=list[i+1];
list[i+1]=list[high];
list[high]=temp;
return (i + 1);
}
void quickSort(struct coor list[], int low, int high)
{
if (low < high)
{
int pi = partition(list, low, high);
// Separately sort elements before
// partition and after partition
quickSort(list, low, pi - 1);
quickSort(list, pi + 1, high);
}
}
OUTPUT:-
Number of records you want to enter? : 10
Before sorting:
X Y Z
13 47 24
24 34 54
24 48 12
18 45 75
24 57 51
28 30 32
14 51 81
18 57 83
3 12 71
12 33 65
After sorting:
X Y Z
3 12 71
12 33 65
13 47 24
14 51 81
18 45 75
18 57 83
24 34 54
24 48 12
24 57 51
28 30 32
Number of records you want to enter? : 100
Before sorting:
X Y Z
13 47 24
24 34 54
24 48 12
18 45 75
24 57 51
28 30 32
14 51 81
18 57 83
3 12 71
12 33 65
25 6 61
26 59 82
3 14 85
6 53 61
3 33 83
25 21 41
8 53 37
8 52 47
28 49 13
27 34 68
21 15 40
16 18 36
22 37 74
29 6 55
28 24 60
10 51 71
5 33 39
9 54 44
23 20 16
11 21 78
9 49 76
14 42 28
24 52 59
12 53 85
29 18 74
13 12 76
6 6 71
7 34 42
7 59 67
1 42 42
4 40 51
19 30 57
21 14 41
18 20 40
6 59 56
9 37 41
28 12 27
21 38 73
11 59 59
15 26 38
14 36 76
10 28 58
2 36 52
3 35 49
17 27 38
17 45 51
7 45 61
10 29 70
15 46 78
29 38 14
17 9 63
28 23 28
28 53 57
4 48 23
28 48 57
15 15 67
20 59 79
19 20 21
11 29 49
5 33 73
19 13 14
14 34 72
6 30 63
18 58 77
25 48 18
29 43 68
20 56 88
28 14 88
25 47 82
17 8 62
7 39 40
20 43 41
9 13 46
28 37 84
29 40 18
27 22 16
26 58 39
17 34 66
16 48 12
5 16 29
20 46 62
1 20 82
20 40 53
16 46 58
4 59 65
10 57 35
26 5 58
20 22 87
2 27 19
9 41 79
After sorting:
X Y Z
1 20 82
1 42 42
2 27 19
2 36 52
3 12 71
3 14 85
3 33 83
3 35 49
4 40 51
4 48 23
4 59 65
5 16 29
5 33 39
5 33 73
6 6 71
6 30 63
6 53 61
6 59 56
7 34 42
7 39 40
7 45 61
7 59 67
8 52 47
8 53 37
9 13 46
9 37 41
9 41 79
9 49 76
9 54 44
10 28 58
10 29 70
10 51 71
10 57 35
11 21 78
11 29 49
11 59 59
12 33 65
12 53 85
13 12 76
13 47 24
14 34 72
14 36 76
14 42 28
14 51 81
15 15 67
15 26 38
15 46 78
16 18 36
16 46 58
16 48 12
17 8 62
17 9 63
17 27 38
17 34 66
17 45 51
18 20 40
18 45 75
18 57 83
18 58 77
19 13 14
19 20 21
19 30 57
20 22 87
20 40 53
20 43 41
20 46 62
20 56 88
20 59 79
21 14 41
21 15 40
21 38 73
22 37 74
23 20 16
24 34 54
24 48 12
24 52 59
24 57 51
25 6 61
25 21 41
25 47 82
25 48 18
26 5 58
26 58 39
26 59 82
27 22 16
27 34 68
28 12 27
28 14 88
28 23 28
28 24 60
28 30 32
28 37 84
28 48 57
28 49 13
28 53 57
29 6 55
29 18 74
29 38 14
29 40 18
29 43 68