Question

Homework 2: Sorting In the second homework you will implement two sorting algorithms of your choice. One sorting algorithm ha

• Reading the input and storing it in the data structure. Sorting the data using the O(n?) algorithm. Sorting the data using

Need some help on this, thanks. Must be done in C language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

solution:

//C program

#include<stdio.h>

struct student{

char name [20];

int ID;

double GPA;

};

void print Data(struct student st[] , int n) {

int i;

for( i=0;i<n;i++){
       printf("Student %d\n",i+1);
      
       printf("Name : %s\n",st[i].name);
       printf("ID : %d\n",st[i].ID);
       printf("GPA : %.2lf\n",st[i].GPA);
       printf("-------------------------------\n\n");
   }
}

void bubbleSort(struct student a[],int n){
   int i,j;
   for(i=n-1;i>=0;i--){
       for(j=0;j<i;j++){
           if(a[j].ID>a[j+1].ID){
               struct student temp=a[j];
               a[j]=a[j+1];
               a[j+1]=temp;
           }
       }
   }
}

void merge(struct student a[],struct student temp[],int l,int m,int r){
   int temp_pos=l,size=r-l+1,left_end=m-1;
   while(l<=left_end&&m<=r){
       if(a[l].GPA<a[m].GPA)temp[temp_pos++]=a[l++];
       else temp[temp_pos++]=a[m++];
   }
   while(l<=left_end)temp[temp_pos++]=a[l++];
   while(m<=r)temp[temp_pos++]=a[m++];
   int i;
   for( i=0;i<size;i++){
       a[r]=temp[r];
       r--;
   }
}

void mergeSort(struct student a[],struct student temp[],int l,int r){
   if(l<r){
       int mid=l+(r-l)/2;
       mergeSort(a,temp,l,mid);
       mergeSort(a,temp,mid+1,r);
       merge(a,temp,l,mid+1,r);
   }
}

int main(){
   int n,i;
   printf("Enter number of students : ");
   scanf("%d",&n);
  
   struct student st[n];
  
   printf("Enter data of %d students\n",n);
   for(i=0;i<n;i++){
       printf("Enter detail of student %d\n",i+1);
       printf("Enter name : ");
       scanf(" %[^\n]%*c", st[i].name);
       printf("Enter Id : ");
       scanf("%d",&st[i].ID);
       printf("Enter GPA : ");
       scanf("%lf",&st[i].GPA);
   }
  
   printf("Sorting on the basis of ID using Bubble Sort (O(n^2)).............\n");
   bubbleSort(st,n);
   printData(st,n);
  
   printf("\n\nSorting on the basis of GPA using Merge Sort (O(nlogn)).............\n");
   struct student temp[n];
   mergeSort(st,temp,0,n-1);
   printData(st,n);
  
   return 0;
}

//sample output

Enter number of students : 5 Enter data of 5 students Enter detail of student 1 Enter name : abc Enter Id : 5 Enter GPA : 4.1

Sorting on the basis of GPA using Merge Sort (Ocnlogn>>.. Student 1 Name : def ID : 3 GPA : 2.10 ----------------------------

please give me thumb up

Add a comment
Know the answer?
Add Answer to:
Need some help on this, thanks. Must be done in C language. Homework 2: Sorting In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that...

    I need the report like this (idea) *Sorting Algorithms: A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the use of other algorithms (such as search and merge algorithms) which require input data to be in sorted lists; it is also often useful for canonical zing data and for producing human-readable output. More formally, the output must satisfy...

  • We need to write a method that, given a key as an argument, returns the next...

    We need to write a method that, given a key as an argument, returns the next in order key found in the binary search tree. If the key given as an argument is not found, the method should still return the next in order key. If the binary tree is empty or all the stored keys are smaller than the argument, then the return value should be empty. For example, using a collection of {10,13,52,67,68,83} stored in the binary search...

  • Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of...

    Need help with program. I'm stuck Objectives: In this assignment, we will practice manipulating lists of data and arranging items in an ascending/descending order. you will also explore storing lists/arrays using different sorting algorithms including, the selection sort, bubble sort, and insertion sort algorithm. Comparison between the three algorithms are made based on the number of comparisons and item assignments (basic operations) each algorithms executes. Background: Ordering the elements of a list is a problem that occurs in many computer...

  • Hello I need help fixing my C++ code. I need to display in the console the...

    Hello I need help fixing my C++ code. I need to display in the console the information saved in the file as well have the content saved in the file output in a fixed position like the screenshot. Thanks. CODE #include<iostream> #include<fstream> #include<iomanip> using namespace std; //main function int main() {    //variable to store student id    int id;       //variables to store old gpa(ogpa), old course credits(occ), new course credits(ncc), current gpa(cur_gpa), cumulative gpa(cum_gpa)    float ogpa,...

  • Project 2 Due April 15° 11:59pm. You could use an the implementation. y of the following programm...

    Project 2 Due April 15° 11:59pm. You could use an the implementation. y of the following programming language to do CH Java Python You will implement a student class with data members (ID, gender, DoB, Major, GPA). You will implement a gradebook class using (Inherit from) TWO of the following data structures. Array Singly Linked list ea Binary Search Tree You need to at least implement the followin gradebook. operations for the 1) FindHighestGPA0 returns the student who has the...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • I need help building a program on microsoft visual studio using c++ language. implement a program...

    I need help building a program on microsoft visual studio using c++ language. implement a program called "charword_freq.cpp" to determine the number of words and the number of occurrences of each letter in a block of text stored in a data file called “mytext.dat”. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma, or the beginning or end of a line. You can assume that the input...

  • Language Java Step 1: Design a class called Student. The Student class should contain the following...

    Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT