idastan97

CSCI151 L29 P1

Nov 11th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct {
  5.     int id;
  6.     char name[12];
  7. } student;
  8.  
  9. void bubbleSortById(student *x[], int size) {
  10.  
  11.     // Put your code here for sorting based on id
  12.     //
  13.     // Hint: Copy the code from the int array version of
  14.     //  bubble sort and paste here, and make the
  15.     //  needed changes from that
  16.     int i;
  17.     for (i=0; i<size-1; i++){
  18.         _Bool swapped=0;
  19.         int j;
  20.         for (j=0; j<size-i-1; j++){
  21.             if (x[j]->id>x[j+1]->id){
  22.                 student *temp=x[j+1];
  23.                 x[j+1]=x[j];
  24.                 x[j]=temp;
  25.                 swapped=1;
  26.             }
  27.         }
  28.         if (!swapped){
  29.             return;
  30.         }
  31.     }
  32.    
  33. }
  34.  
  35. _Bool nameCompare(char name1[], char name2[]) {
  36.  
  37.     int i = 0;
  38.     do {
  39.         if (name1[i] < name2[i]) {
  40.             return 1;
  41.         } else if (name1[i] > name2[i]) {
  42.             return 0;
  43.         }
  44.         i++;
  45.     } while (name1[i] != '\0' || name2[i] != '\0');
  46.  
  47.     return 0;
  48. }
  49.  
  50. int partitionByName(student *x[], int first, int last) {
  51.  
  52.     // Put your code here for partitioning based on name
  53.     //
  54.     // Hint: Copy the code from the int array version of
  55.     //  partition and paste here, and make the needed
  56.     //  changes from that
  57.     int pivot=first;
  58.     int up=first, down=last;
  59.     while (up<down){
  60.         while (!nameCompare(x[pivot]->name,x[up]->name) && up<last){
  61.             up++;
  62.         }
  63.         while (nameCompare(x[pivot]->name,x[down]->name)){
  64.             down--;
  65.         }
  66.         if (up<down){
  67.             student *temp=x[up];
  68.             x[up]=x[down];
  69.             x[down]=temp;
  70.         }
  71.     }
  72.     student *temp2=x[down];
  73.     x[down]=x[pivot];
  74.     x[pivot]=temp2;
  75.     return down;
  76.     return 0;
  77. }
  78.  
  79. void quicksortByName(student *x[], int first, int last) {
  80.  
  81.     // Put your code here for sorting based on name; use
  82.     // the nameCompare function below
  83.     //
  84.     // Hint: Copy the code from the int array version of
  85.     //  quicksort and paste here, and make the
  86.     //  needed changes from that
  87.     if (first<last){
  88.         int pivotindex=partitionByName(x, first, last);
  89.         quicksortByName(x, first, pivotindex-1);
  90.         quicksortByName(x, pivotindex+1, last);
  91.     }
  92. }
  93.  
  94.  
  95.  
  96. void printArray(student *x[], int size) {
  97.  
  98.     int i;
  99.     for (i = 0; i < size; i++) {
  100.         printf("%8i %12s \n", x[i]->id, x[i]->name);
  101.     }
  102.     printf("\n");
  103. }
  104.  
  105. int main() {
  106.  
  107.     student *roster[8];
  108.  
  109.     // Opening the input file
  110.     FILE *file = fopen("records.txt", "r");
  111.     if (file == NULL) {
  112.         printf("Cannot find file.");
  113.         exit(1);
  114.     }
  115.     printf("File opened successfully.\n");
  116.  
  117.     // Reading the records into the roster from the file
  118.     int i;
  119.     for (i = 0; i < 8; i++) {
  120.         roster[i] = malloc(sizeof(student));
  121.         fscanf(file, "%i %s \n", &roster[i]->id, roster[i]->name);
  122.     };
  123.     fclose(file);
  124.  
  125.     // Printing out the roster, in the initial order
  126.     printArray(roster, 8);
  127.  
  128.     // Sorting the array by id (hopefully)
  129.     bubbleSortById(roster, 8);
  130.  
  131.     // Printing out the roster, after sorting by id
  132.     printArray(roster, 8);
  133.  
  134.     // Sorting the array by name (hopefully)
  135.     quicksortByName(roster, 0, 7);
  136.  
  137.     // Printing out the roster, after sorting by name
  138.     printArray(roster, 8);
  139.  
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment