Advertisement
NB52053

Nadui_LAB2

Oct 22nd, 2016
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include<math.h>
  5.  
  6.  
  7. void RunSort(void);
  8. void InsertionSort(void);
  9. void SelectionSort(void);
  10. void BubbleSort(void);
  11. void genNumtoFile(void);
  12. void savetofile(void);
  13.  
  14. int totalNum = 5, values[100];
  15.  
  16.  
  17.  
  18. int main(){
  19.  
  20.  
  21.     genNumtoFile();
  22.     RunSort();
  23.     savetofile();
  24. }
  25.  
  26.  
  27. void RunSort(){
  28.  
  29.     int choice;
  30.     time_t start, end;
  31.     start = time(NULL);
  32.  
  33.     while (1){
  34.         printf("\n\nIn which method you want to sort?\n\n");
  35.         printf(" 1> Insertion Sort\n");
  36.         printf(" 2> Selection Sort\n");
  37.         printf(" 3> Bubble Sort\n");
  38.         printf(" 4> Save\n");
  39.         printf(" 5> To terminate!\n");
  40.  
  41.  
  42.  
  43.         scanf("%d", &choice);
  44.  
  45.  
  46.         end = time(NULL);
  47.         double timePassed = difftime(end, start);
  48.  
  49.  
  50.         switch (choice){
  51.  
  52.         case 1:
  53.             InsertionSort();
  54.             printf("Time taken: %.2f seconds\n", timePassed);
  55.             break;
  56.         case 2:
  57.             SelectionSort();
  58.             printf("Time taken: %.2f seconds\n", timePassed);
  59.             break;
  60.         case 3:
  61.             BubbleSort();
  62.             printf("Time taken: %.2f seconds\n", timePassed);
  63.             break;
  64.         case 5:
  65.             exit(0);
  66.         case 4:
  67.             savetofile();
  68.             printf("\nData is saved successfully\n");
  69.             break;
  70.  
  71.         }
  72.     }
  73.  
  74.  
  75. }
  76.  
  77. void genNumtoFile(){
  78.  
  79.     int i;
  80.  
  81.     FILE *fp;
  82.     errno_t err = fopen_s(&fp, "valuesGenerated.txt", "w");
  83.  
  84.     for (i = 0; i<totalNum; i++){
  85.         values[i] = rand() % 1000;
  86.         fprintf_s(fp, "%d\n", values[i]);
  87.     }
  88.     fclose(fp);
  89.  
  90.     printf("Randomly generated values are:\n \n");
  91.     for (i = 0; i < totalNum; i++){
  92.         printf("\t%d", values[i]);
  93.     }
  94.  
  95. }
  96.  
  97. void InsertionSort(){
  98.     int i, t, j;
  99.     for (j = 1; j< totalNum; j++) {
  100.         t = values[j];
  101.         i = j - 1;
  102.  
  103.         while (i >= 0 && values[i] > t) {
  104.             values[i + 1] = values[i];
  105.             i = i - 1;
  106.         }
  107.  
  108.         values[i + 1] = t;
  109.     }
  110.     printf("\nInsertion SORT : ");
  111.     for (i = 0; i< totalNum; i++) {
  112.         printf(" %d ", values[i]);
  113.     }
  114.  
  115.     printf("\n");
  116.  
  117. }
  118.  
  119. void SelectionSort(){
  120.  
  121.     int i, j, t, k, tmp;
  122.  
  123.     for (j = totalNum - 1; j >= 2; j--){
  124.         t = 1;
  125.  
  126.         for (k = 2; k <= j; k++){
  127.             if (values[k] > values[t]){
  128.                 t = k;
  129.  
  130.             }
  131.  
  132.         }
  133.  
  134.         tmp = values[t];
  135.         values[t] = values[j];
  136.         values[j] = tmp;
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.     printf("\nSelection Sort : ");
  143.     for (i = 0; i< totalNum; i++) {
  144.         printf(" %d ", values[i]);
  145.     }
  146.     printf("\n");
  147. }
  148.  
  149. void BubbleSort(){
  150.  
  151.    
  152.     int i, j, temp;
  153.  
  154.     for (i = 0; i < totalNum - 1; i++){
  155.         for (j = 0; j < totalNum - 1; j++){
  156.  
  157.             if (values[j] > values[j + 1]) {
  158.  
  159.                 temp = values[j];
  160.                 values[j] = values[j + 1];
  161.                 values[j + 1] = temp;
  162.             }
  163.         }
  164.     }
  165.  
  166.  
  167.     printf("\n\n Bubble sort  : ");
  168.     for (i = 0; i < totalNum; i++){
  169.  
  170.         printf("%d ", values[i]);
  171.     }
  172.     printf("\n");
  173. }
  174.  
  175. void savetofile(){
  176.  
  177.  
  178.     int i;
  179.  
  180.     FILE *fp;
  181.     errno_t err = fopen_s(&fp, "Sortedvalues.txt", "w");
  182.  
  183.     for (i = 0; i<totalNum; i++){
  184.         fprintf_s(fp, "%d\n", values[i]);
  185.     }
  186.     fclose(fp);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement