C language bubble sort and fast sort algorithm using examples

  • 2020-04-02 03:14:00
  • OfStack

Bubble sort

Title description:

      A one-dimensional array is used to store student Numbers and grades, and then the output is sorted by grades.

Input:

      The first line of input includes an integer N(1) < = N < =100), represents the number of students.
      The next N rows each contain two integers p and q, representing each student's student number and grade, respectively.

Output:

      According to the students' grades from small to large, and the sorted information of the students will be printed out.
      If the student's grade is the same, the student number is ranked from the smallest to the largest.

Sample input:

      3
      1, 90
      2, 87
      3, 92

Sample output:

      2, 87
      1, 90
      3, 92

Code:

         


  #include <stdio.h> 
    #include <stdlib.h> 
     
    struct student 
    { 
      int number; 
      int score; 
    }; 
     
    int main() 
    { 
      struct student students[101]; 
      int n, i, j; 
      struct student temp; 
     
      while(scanf("%d",&n) != EOF) 
      { 
        //Receive data
        for(i = 0; i < n; i++) 
        { 
          scanf("%d%d",&students[i].number,&students[i].score); 
        } 
     
        //Bubble sort
        for(i = 0; i < n - 1; i ++) 
        { 
          for(j = 0; j < n - i - 1; j ++) 
          { 
            if(students[j].score > students[j + 1].score) 
            { 
              temp = students[j]; 
              students[j] = students[j + 1]; 
              students[j + 1] = temp; 
            }else if(students[j].score == students[j + 1].score) 
            { 
              if(students[j].number > students[j + 1].number) 
              { 
                temp = students[j]; 
                students[j] = students[j + 1]; 
                students[j + 1] = temp; 
              } 
            } 
          } 
        } 
     
        //Output sort result
        for(i = 0; i < n; i ++) 
        { 
          printf("%d %dn",students[i].number,students[i].score); 
        } 
      } 
       
      return 0; 
    } 

Quicksort method


Title description:

              There are N students' data, and the students' data is sorted according to the score. If the score is the same, the students' data is sorted according to the alphabetical order of the name characters, and if the alphabetical order of the name is the same, the students' age is sorted according to the output of the information sorted by N students.

Input:

              There are multiple sets of test data, and the first row of each set input has an integer N (N) < =1000), and the next N rows contain data for N students.
              The data for each student includes name (strings of length not exceeding 100), age (integers), and grade (positive Numbers less than or equal to 100).

Output:

              Student information is sorted by grade, and those with the same grade are sorted by alphabetical order of name.
              Then output the student information in the following format:
              Name age score

Sample input:

      3
      ABC 20, 99
      BCD 19, 97
      Bed 20, 97

Sample output:

      BCD 19, 97
      Bed 20, 97
      ABC 20, 99

code


  #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
     
    struct student{ 
      char name[101]; 
      int age; 
      int grade; 
    }; 
     
    int partition(struct student *A, int left, int right); 
    void quicksort(struct student *A, int begin, int end); 
     
    int main() 
    { 
      struct student students[1001]; 
      int i, n; 
     
      while(scanf("%d",&n) != EOF) 
      { 
        //Student grade assignment
        for(i = 0; i < n; i ++) 
        { 
          scanf("%s%d%d",students[i].name, &students[i].age, &students[i].grade); 
        } 
     
        //Quick sort
        quicksort(students, 0, n-1); 
     
        //A printout
        for(i = 0; i < n; i ++) 
        { 
          printf("%s %d %dn",students[i].name, students[i].age, students[i].grade); 
        } 
      } 
     
      return 0; 
    } 
     
    void quicksort(struct student *A, int begin, int end) 
    { 
      int pivot; 
     
      if(begin < end) 
      { 
        pivot = partition(A, begin, end); 
        quicksort(A, begin, pivot - 1); 
        quicksort(A, pivot + 1, end); 
      } 
    } 
     
    int partition(struct student *A, int left, int right) 
    { 
      struct student stand = A[left]; 
     
      while(left < right) 
      { 
        while(left < right && (A[right].grade > stand.grade || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) > 0) || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) == 0 && A[right].age > stand.age ) ) ) 
        { 
          right --; 
        } 
        if(left < right) 
        { 
          A[left ++] = A[right]; 
        } 
     
        while(left < right && (A[left].grade < stand.grade || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) < 0) || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) == 0 && A[left].age < stand.age ) ) ) 
        { 
          left ++; 
        } 
        if(left < right) 
        { 
          A[right --] = A[left]; 
        } 
      } 
      A[left] = stand; 
      return left; 
    } 


Related articles: