Question

In: Computer Science

The task is to sort structs in array. The struct is struct coor{ int x; int...

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.

Solutions

Expert Solution

#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


Related Solutions

C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char...
Implement stack in C Struct: struct patients{ int id; int severity; char *firstName; char *lastName; char *state; int time_spent; }; Given Array: struct patients* patientsArray[4] = {&p1, &p2, &p3, &p4}; Create two functions one for pushing this array to the stack and one for popping the variables such as p1 p2 p3 p4 by its ID
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array...
Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3: Suppose you are given the following array X = [7, 9, 1, 6] Sort the array in ascending order using the selction sort algorithm. Write the state of the array after each pass. Pass1: Pass2: Pass3:
using javascript and without using javascript sort. Sort an array from lowest to highest const x...
using javascript and without using javascript sort. Sort an array from lowest to highest const x = [201,28,30,-5] ​function sort(array){ // put your code here return //the answer } ​ sort(x) // output: [-5,28,30,201]
Consider the following struct that represents a node within a binary tree: struct Node { int...
Consider the following struct that represents a node within a binary tree: struct Node { int data; // Data of interest Node *left // Link to left subtree (nullptr if none) Node *right ; // Link to right subtree (nullptr if none) }; Complete the following function that computes the number of elements in a binary tree: // Counts the number of elements in the binary tree to which t points. // Returns the number of elements. int size(Node *t)...
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name...
Using Union technique, define two structs employee_record and student_record. The employee_record struct will contain the name (size 50) and the salary (double) of an employee. The student_record struct will have the name (size 30), Id number (int) of a student, and the grade (int). Include these two structs in a union called 'person'. Using file redirection called input.txt, at first, ask the user for the employee's information and print them from the main() and the size of the union. Then...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to...
Struct PERSON Assignment Outcomes: Demonstrate the ability to create structs using typedef Demonstrate the ability to create an array of structs Program Specifications: DESIGN and IMPLEMENT a program that will CREATE and use three different variables of type PERSON. Create a struct using the typedef command for a DATE. Create a struct for a PERSON with the following fields. name [this will be a string] birthdate [this will be a DATE] gender [this will be a char] annualIncome [this will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT