Advertisement
frentzy

Selection sort, mai pe easy

Jun 18th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #pragma warning (disable:4996)
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <iostream>
  5. #include <string.h>
  6. using namespace std;
  7. #define MAX_STRINGS 32
  8. #define MAX_STRING_LEN 200
  9.  
  10. void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN]);
  11. void Afisare(char strings[][MAX_STRING_LEN]) {
  12.     cout << "-------------------------\n";
  13.     for (int index = 0; index < MAX_STRINGS; index++)
  14.     {
  15.         //  cout << "hihi" << endl;
  16.         printf("%s\n", strings[index]);
  17.     }
  18.     cout << "--------------------------\n";
  19. }
  20. void main()
  21. {
  22.     int index;
  23.     char strings[MAX_STRINGS][MAX_STRING_LEN];
  24.  
  25.     /* Get input */
  26.     printf("Enter %d strings.\n", MAX_STRINGS);
  27.     for (index = 1; index < 2; index++)
  28.     {
  29.         strcpy(strings[0], "push");
  30.         strings[0][sizeof(strings[0]) - 1] = '\0';
  31.         /*printf("Input string %d : ", index);
  32.         scanf("%199s", strings[index]);     // limit the width so we don't go past the buffer
  33.         strings[index][sizeof(strings[index]) - 1] = '\0';
  34.         */
  35.         strcpy(strings[1], "poll");
  36.         strings[1][sizeof(strings[1]) - 1] = '\0';
  37.  
  38.         strcpy(strings[2], "iran");
  39.         strings[2][sizeof(strings[2]) - 1] = '\0';
  40.  
  41.         strcpy(strings[3], "iraq");
  42.         strings[3][sizeof(strings[3]) - 1] = '\0';
  43.  
  44.         strcpy(strings[4], "bank");
  45.         strings[4][sizeof(strings[4]) - 1] = '\0';
  46.  
  47.         strcpy(strings[5], "fdic");
  48.         strings[5][sizeof(strings[5]) - 1] = '\0';
  49.  
  50.         strcpy(strings[6], "axis");
  51.         strings[6][sizeof(strings[6]) - 1] = '\0';
  52.  
  53.         strcpy(strings[7], "evil");
  54.         strings[7][sizeof(strings[7]) - 1] = '\0';
  55.  
  56.         strcpy(strings[8], "lame");
  57.         strings[8][sizeof(strings[8]) - 1] = '\0';
  58.  
  59.         strcpy(strings[9], "duck");
  60.         strings[9][sizeof(strings[9]) - 1] = '\0';
  61.  
  62.         strcpy(strings[10], "town");
  63.         strings[10][sizeof(strings[10]) - 1] = '\0';
  64.  
  65.         strcpy(strings[11], "hall");
  66.         strings[11][sizeof(strings[11]) - 1] = '\0';
  67.  
  68.         strcpy(strings[12], "iraq");
  69.         strings[12][sizeof(strings[12]) - 1] = '\0';
  70.  
  71.         strcpy(strings[13], "iraq");
  72.         strings[13][sizeof(strings[13]) - 1] = '\0';
  73.  
  74.         strcpy(strings[14], "bull");
  75.         strings[14][sizeof(strings[14]) - 1] = '\0';
  76.  
  77.         strcpy(strings[15], "bear");
  78.         strings[15][sizeof(strings[15]) - 1] = '\0';
  79.  
  80.         strcpy(strings[16], "veto");
  81.         strings[16][sizeof(strings[16]) - 1] = '\0';
  82.  
  83.         strcpy(strings[17], "race");
  84.         strings[17][sizeof(strings[17]) - 1] = '\0';
  85.  
  86.         strcpy(strings[18], "free");
  87.         strings[18][sizeof(strings[18]) - 1] = '\0';
  88.  
  89.         strcpy(strings[19], "whip");
  90.         strings[19][sizeof(strings[19]) - 1] = '\0';
  91.  
  92.         strcpy(strings[20], "vice");
  93.         strings[20][sizeof(strings[20]) - 1] = '\0';
  94.  
  95.         strcpy(strings[21], "jobs");
  96.         strings[21][sizeof(strings[21]) - 1] = '\0';
  97.  
  98.         strcpy(strings[22], "spin");
  99.         strings[22][sizeof(strings[22]) - 1] = '\0';
  100.  
  101.         strcpy(strings[23], "flag");
  102.         strings[23][sizeof(strings[23]) - 1] = '\0';
  103.  
  104.         strcpy(strings[24], "pork");
  105.         strings[24][sizeof(strings[24]) - 1] = '\0';
  106.  
  107.         strcpy(strings[25], "vote");
  108.         strings[25][sizeof(strings[25]) - 1] = '\0';
  109.  
  110.         strcpy(strings[26], "bill");
  111.         strings[26][sizeof(strings[26]) - 1] = '\0';
  112.  
  113.         strcpy(strings[27], "cage");
  114.         strings[27][sizeof(strings[27]) - 1] = '\0';
  115.  
  116.         strcpy(strings[28], "chad");
  117.         strings[28][sizeof(strings[28]) - 1] = '\0';
  118.  
  119.         strcpy(strings[29], "iraq");
  120.         strings[29][sizeof(strings[29]) - 1] = '\0';
  121.  
  122.         strcpy(strings[30], "bush");
  123.         strings[30][sizeof(strings[30]) - 1] = '\0';
  124.  
  125.         strcpy(strings[31], "iraq");
  126.         strings[31][sizeof(strings[31]) - 1] = '\0';
  127.  
  128.     }
  129.  
  130.     InsertionSort(strings);
  131.  
  132.     printf("\nThe input set, in alphabetical order:\n");
  133.     Afisare(strings);
  134.     _getch();
  135. }
  136.  
  137. void InsertionSort(char list[MAX_STRINGS][MAX_STRING_LEN])
  138. {
  139.     for (int i = 0; i < MAX_STRINGS; i++)
  140.     {
  141.         Afisare(list);
  142.         //  int j = i;
  143.  
  144.         /*  while (j > 0 && strcmp(list[j - 1], list[j]) > 0)
  145.             {
  146.                 cout << "Se verifica: " << list[j - 1] << " cu " << list[j]<<endl;
  147.                 char tmp[MAX_STRING_LEN];
  148.                 strncpy(tmp, list[j - 1], sizeof(tmp) - 1);
  149.                 tmp[sizeof(tmp) - 1] = '\0';
  150.  
  151.                 strncpy(list[j - 1], list[j], sizeof(list[j - 1]) - 1);
  152.                 list[j - 1][sizeof(list[j - 1]) - 1] = '\0';
  153.  
  154.                 strncpy(list[j], tmp, sizeof(list[j]));
  155.                 list[j][sizeof(list[j]) - 1] = '\0';
  156.                 Afisare(list);
  157.                 --j;
  158.             }
  159.         }*/
  160.         for (int j = i + 1; j < MAX_STRINGS; j++) {
  161.         //  cout << "Se verifica: " << list[j] << " cu " << list[j+1] << endl;
  162.             if (strcmp(list[i], list[j]) >= 1) {
  163.                
  164.                 char tmp[MAX_STRING_LEN];
  165.                 /*
  166.                 aux = a;
  167.                 a = b;
  168.                 b= aux;
  169.                 */
  170.  
  171.                 strncpy(tmp, list[i], sizeof(tmp) - 1);
  172.                 tmp[sizeof(tmp) - 1] = '\0';
  173.  
  174.                 strncpy(list[i], list[j], sizeof(list[i]) - 1);
  175.                 list[i][sizeof(list[i]) - 1] = '\0';
  176.  
  177.                 strncpy(list[j], tmp, sizeof(list[i]));
  178.                 list[j][sizeof(list[j]) - 1] = '\0';
  179.             //  Afisare(list);
  180.             }
  181.         }
  182.     }
  183.    
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement