Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.52 KB | None | 0 0
  1. #include "Sorting.h"
  2.  
  3. void dataInit(Data* data, int arraySize, int repeat)
  4. {
  5.     data->arraySize = arraySize;
  6.     data->array = (int*)malloc(data->arraySize * sizeof(int));
  7. }
  8.  
  9. void dataFree(Data* data)
  10. {
  11.     free(data->array);
  12. }
  13.  
  14. void assignRandomValues(int* array, int arraySize)
  15. {
  16.     int i;
  17.     for (i = 0; i < arraySize; i++)
  18.         array[i] = rand() % 101;
  19. }
  20.  
  21. void copyValues(int* sourceArray, int* arrayToCopyTo, int arraySize)
  22. {
  23.     int i;
  24.     for (i = 0; i < arraySize; i++)
  25.         arrayToCopyTo[i] = sourceArray[i];
  26. }
  27.  
  28. void swapElementsOfArray(int* firstElement, int* secondElement)
  29. {
  30.     int tmp = *secondElement;
  31.     *secondElement = *firstElement;
  32.     *firstElement = tmp;
  33. }
  34.  
  35. void bubbleSorting(int* array, int arraySize, int* comparisons, int* substitutions)
  36. {
  37.     int i, j;
  38.     for (i = 0; i < arraySize - 1; i++)
  39.         for (j = 0; j < arraySize - i - 1; j++)
  40.         {
  41.             (*comparisons)++;
  42.             if (array[j] > array[j + 1])
  43.             {
  44.                 swapElementsOfArray(&array[j], &array[j + 1]);
  45.                 (*substitutions) += 2;
  46.             }
  47.         }
  48. }
  49.  
  50. void insertionSorting(int* array, int arraySize, int* comparisons, int* substitutions)
  51. {
  52.     int i, j;
  53.     int tmp;
  54.     for (i = 1; i < arraySize; i++)
  55.     {
  56.         tmp = array[i];
  57.         j = i - 1;
  58.         while (j >= 0 && array[j] > tmp)
  59.         {
  60.             (*comparisons)++;
  61.             array[j + 1] = array[j];
  62.             (*substitutions)++;
  63.             j = j - 1;
  64.         }
  65.         array[j + 1] = tmp;
  66.         (*substitutions)++;
  67.     }
  68. }
  69.  
  70. void shellSorting(int* array, int arraySize, int* comparisons, int* substitutions)
  71. {
  72.     int i, j, l;
  73.     int k = 1;
  74.     int interval = arraySize / 2;
  75.     while (interval >= 1)
  76.     {
  77.         for (l = 0; l < interval; l++)
  78.         {
  79.             for (i = l; i < arraySize - 1; i += interval)
  80.                 for (j = l; j < arraySize - i - 1; j += interval)
  81.                 {
  82.                     (*comparisons)++;
  83.                     if (j + interval < arraySize)
  84.                         if (array[j] > array[j + interval])
  85.                         {
  86.                             swapElementsOfArray(&array[j], &array[j + interval]);
  87.                             (*substitutions) += 2;
  88.                         }
  89.                 }
  90.         }
  91.         k++;
  92.         interval = arraySize / pow(2.0, k);
  93.     }
  94.     bubbleSorting(array, arraySize, comparisons, substitutions);
  95. }
  96.  
  97. int quickSort_Partition(int* array, int firstIndex, int lastIndex, int* comparisons, int* substitutions)
  98. {
  99.     int pivot = array[firstIndex];
  100.     int i = firstIndex, j;
  101.     for (j = firstIndex + 1; j <= lastIndex; j++)
  102.     {
  103.         (*comparisons)++;
  104.         if (array[j] < pivot)
  105.         {
  106.             i++;
  107.             swapElementsOfArray(&array[i], &array[j]);
  108.             (*substitutions) += 2;
  109.         }
  110.     }
  111.     swapElementsOfArray(&array[i], &array[firstIndex]);
  112.     (*substitutions) += 2;
  113.     return i;
  114.  
  115. }
  116.  
  117. void quickSorting(int* array, int firstIndex, int lastIndex, int* comparisons, int* substitutions)
  118. {
  119.     int partitioningIndex;
  120.     if (firstIndex < lastIndex)
  121.     {
  122.         partitioningIndex = quickSort_Partition(array, firstIndex, lastIndex, comparisons, substitutions);
  123.         quickSorting(array, firstIndex, partitioningIndex, comparisons, substitutions);
  124.         quickSorting(array, partitioningIndex + 1, lastIndex, comparisons, substitutions);
  125.     }
  126. }
  127.  
  128. void printArray(int* array, int arraySize)
  129. {
  130.     int i;
  131.     for (i = 0; i < arraySize; i++)
  132.         printf("%d, ", array[i]);
  133.     printf("\n\n");
  134. }
  135.  
  136. void printArrayChar(char* array[], int arraySize)
  137. {
  138.     int i;
  139.     for (i = 0; i < arraySize; i++)
  140.         printf("%s, ", array[i]);
  141.     printf("\n\n");
  142. }
  143.  
  144. int getRandomSize(int approxArraySize)
  145. {
  146.     int x = approxArraySize / 10, result;
  147.     result = (rand() % (2 * x)) - x;
  148.     result += approxArraySize;
  149.     return result;
  150. }
  151.  
  152. void checkMinMax(Data* data, int comps, int subs)
  153. {
  154.     if (data->minComparisons > comps)
  155.         data->minComparisons = comps;
  156.     if (data->maxComparisons < comps)
  157.         data->maxComparisons = comps;
  158.     if (data->minSubstitutions > subs)
  159.         data->minSubstitutions = subs;
  160.     if (data->maxSubstitutions < subs)
  161.         data->maxSubstitutions = subs;
  162.  
  163.     data->sumOfComparisons += (long)comps;
  164.     data->sumOfSubstitutions += (long)subs;
  165. }
  166.  
  167. void compareSortingAlgorithms(int sizeOfSortedArray, int numberOfRepeats)
  168. {
  169.     int* originalArray;
  170.     Data bubbleSort, insertionSort, shellSort, quickSort;
  171.     int arraySize;
  172.     const int approxArraySize = sizeOfSortedArray;
  173.     const int repeats = numberOfRepeats;
  174.     int i;
  175.     int bbSortComps = 0, insSortComps = 0, shllSortComps = 0, qckSortComps = 0;
  176.     int bbSortSubs = 0, insSortSubs = 0, shllSortSubs = 0, qckSortSubs = 0;
  177.  
  178.     srand((int)time(NULL));
  179.  
  180.     bubbleSort.sumOfComparisons = bubbleSort.sumOfSubstitutions = insertionSort.sumOfComparisons = insertionSort.sumOfSubstitutions = shellSort.sumOfComparisons = shellSort.sumOfSubstitutions = quickSort.sumOfComparisons = quickSort.sumOfSubstitutions = 0;
  181.  
  182.     for (i = 0; i < repeats; i++)
  183.     {
  184.         arraySize = getRandomSize(approxArraySize);
  185.         originalArray = (int*)malloc(arraySize * sizeof(int));
  186.         assignRandomValues(originalArray, arraySize);
  187.  
  188.         dataInit(&bubbleSort, arraySize, i);
  189.         dataInit(&insertionSort, arraySize, i);
  190.         dataInit(&shellSort, arraySize, i);
  191.         dataInit(&quickSort, arraySize, i);
  192.  
  193.         bbSortComps = insSortComps = shllSortComps = qckSortComps = 0;
  194.         bbSortSubs = insSortSubs = shllSortSubs = qckSortSubs = 0;
  195.  
  196.         copyValues(originalArray, bubbleSort.array, bubbleSort.arraySize);
  197.         copyValues(originalArray, insertionSort.array, insertionSort.arraySize);
  198.         copyValues(originalArray, shellSort.array, shellSort.arraySize);
  199.         copyValues(originalArray, quickSort.array, quickSort.arraySize);
  200.  
  201.         bubbleSorting(bubbleSort.array, bubbleSort.arraySize, &bbSortComps, &bbSortSubs);
  202.         insertionSorting(insertionSort.array, insertionSort.arraySize, &insSortComps, &insSortSubs);
  203.         shellSorting(shellSort.array, shellSort.arraySize, &shllSortComps, &shllSortSubs);
  204.         quickSorting(quickSort.array, 0, quickSort.arraySize - 1U, &qckSortComps, &qckSortSubs);
  205.  
  206.         if (i == 0)
  207.         {
  208.             bubbleSort.minComparisons = bubbleSort.maxComparisons = bbSortComps;
  209.             bubbleSort.minSubstitutions = bubbleSort.maxSubstitutions = bbSortSubs;
  210.             insertionSort.minComparisons = insertionSort.maxComparisons = insSortComps;
  211.             insertionSort.minSubstitutions = insertionSort.maxSubstitutions = insSortSubs;
  212.             shellSort.minComparisons = shellSort.maxComparisons = shllSortComps;
  213.             shellSort.minSubstitutions = shellSort.maxSubstitutions = shllSortSubs;
  214.             quickSort.minComparisons = quickSort.maxComparisons = qckSortComps;
  215.             quickSort.minSubstitutions = quickSort.maxSubstitutions = qckSortSubs;
  216.         }
  217.         else
  218.         {
  219.             checkMinMax(&bubbleSort, bbSortComps, bbSortSubs);
  220.             checkMinMax(&insertionSort, insSortComps, insSortSubs);
  221.             checkMinMax(&shellSort, shllSortComps, shllSortSubs);
  222.             checkMinMax(&quickSort, qckSortComps, qckSortSubs);
  223.         }
  224.  
  225.         dataFree(&bubbleSort);
  226.         dataFree(&insertionSort);
  227.         dataFree(&shellSort);
  228.         dataFree(&quickSort);
  229.  
  230.         free(originalArray);
  231.     }
  232.     bubbleSort.avgComparisons = bubbleSort.sumOfComparisons / repeats;
  233.     insertionSort.avgComparisons = insertionSort.sumOfComparisons / repeats;
  234.     shellSort.avgComparisons = shellSort.sumOfComparisons / repeats;
  235.     quickSort.avgComparisons = quickSort.sumOfComparisons / repeats;
  236.     bubbleSort.avgSubstitutions = bubbleSort.sumOfSubstitutions / repeats;
  237.     insertionSort.avgSubstitutions = insertionSort.sumOfSubstitutions / repeats;
  238.     shellSort.avgSubstitutions = shellSort.sumOfSubstitutions / repeats;
  239.     quickSort.avgSubstitutions = quickSort.sumOfSubstitutions / repeats;
  240.  
  241.     printf("Bubblesort:\n\tMin porownan: %d\n\tMax porownan: %d\n\tSrednia porownan: %d\n\n\tMin podstawien: %d\n\tMax podstawien: %d\n\tSrednia podstawien: %d\n\n", bubbleSort.minComparisons, bubbleSort.maxComparisons, bubbleSort.avgComparisons, bubbleSort.minSubstitutions, bubbleSort.maxSubstitutions, bubbleSort.avgSubstitutions);
  242.     printf("Insertionsort:\n\tMin porownan: %d\n\tMax porownan: %d\n\tSrednia porownan: %d\n\n\tMin podstawien: %d\n\tMax podstawien: %d\n\tSrednia podstawien: %d\n\n", insertionSort.minComparisons, insertionSort.maxComparisons, insertionSort.avgComparisons, insertionSort.minSubstitutions, insertionSort.maxSubstitutions, insertionSort.avgSubstitutions);
  243.     printf("Shellsort:\n\tMin porownan: %d\n\tMax porownan: %d\n\tSrednia porownan: %d\n\n\tMin podstawien: %d\n\tMax podstawien: %d\n\tSrednia podstawien: %d\n\n", shellSort.minComparisons, shellSort.maxComparisons, shellSort.avgComparisons, shellSort.minSubstitutions, shellSort.maxSubstitutions, shellSort.avgSubstitutions);
  244.     printf("Quicksort:\n\tMin porownan: %d\n\tMax porownan: %d\n\tSrednia porownan: %d\n\n\tMin podstawien: %d\n\tMax podstawien: %d\n\tSrednia podstawien: %d\n\n", quickSort.minComparisons, quickSort.maxComparisons, quickSort.avgComparisons, quickSort.minSubstitutions, quickSort.maxSubstitutions, quickSort.avgSubstitutions);
  245.  
  246.     system("pause");
  247. }
  248.  
  249. void Heapify(char* array[], int i, int heapSize)
  250. {
  251.     int left, right, largest = i;
  252.     char* tmp;
  253.  
  254.     left = 2 * i + 1;
  255.     right = 2 * i + 2;
  256.  
  257.     if (left < heapSize && strcmp(array[left], array[largest]) == 1)
  258.         largest = left;
  259.  
  260.     if (right < heapSize && strcmp(array[right], array[largest]) == 1)
  261.         largest = right;
  262.  
  263.     if (largest != i)
  264.     {
  265.         tmp = array[i];
  266.         array[i] = array[largest];
  267.         array[largest] = tmp;
  268.         Heapify(array, largest, heapSize);
  269.     }
  270. }
  271.  
  272. void BuildHeap(char* array[], int size)
  273. {
  274.     int i;
  275.     for (i = (size / 2) - 1; i >= 0; i--)
  276.         Heapify(array, i, size);
  277. }
  278.  
  279. void HeapSort(char* array[], int size)
  280. {
  281.     int i, heapSize = size;
  282.     char* tmp;
  283.     BuildHeap(array, size);
  284.     for (i = size - 1; i >= 0; i--)
  285.     {
  286.         tmp = array[0];
  287.         array[0] = array[i];
  288.         array[i] = tmp;
  289.         Heapify(array, 0, --heapSize);
  290.     }
  291. }
  292.  
  293. void HeapSortHandle()
  294. {
  295.     char* stringArray[] = { "Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M" };
  296.  
  297.     printArrayChar(stringArray, sizeof(stringArray) / sizeof(stringArray[0]));
  298.     HeapSort(stringArray, sizeof(stringArray) / sizeof(stringArray[0]));
  299.     printArrayChar(stringArray, sizeof(stringArray) / sizeof(stringArray[0]));
  300.     menuPause();
  301.  
  302. }
  303.  
  304. void CountingSort(int array[], int arraySize, int maxNaturalNumber)
  305. {
  306.     int i, j = 0;
  307.     int* counterArray = (int*)calloc(maxNaturalNumber + 1, sizeof(int));
  308.     if (counterArray != NULL)
  309.     {
  310.         for (i = 0; i < arraySize; i++)
  311.             counterArray[array[i]]++;
  312.         for (i = 0; i <= maxNaturalNumber; i++)
  313.             while (counterArray[i]--)
  314.                 array[j++] = i;
  315.         free(counterArray);
  316.     }
  317. }
  318.  
  319. void CountingSortHandle(int arraySize, int maxNaturalNumber)
  320. {
  321.     int* array, i;
  322.     array = (int*)calloc(arraySize, sizeof(int));
  323.     if (array != NULL)
  324.         for (i = 0; i < arraySize; i++)
  325.             array[i] = rand() % (maxNaturalNumber + 1);
  326.     printArray(array, arraySize);
  327.     CountingSort(array, arraySize, maxNaturalNumber);
  328.     printArray(array, arraySize);
  329.     menuPause();
  330. }
  331.  
  332. void Sorting_Menu()
  333. {
  334.     int size, repeats;
  335.     int option = 1;
  336.     int i, k;
  337.     char* programName[] =
  338.     {
  339.         "Wyjdz",
  340.         "Porownanie algorytmow (BubbleSort, InsertionSort, ShellSort, QuickSort) [zadanie 21]",
  341.         "Implementacja sortowania przez kopcowanie [zadanie 22]",
  342.         "Implementacja sortowania przez zliczanie [zadanie 23]"
  343.     };
  344.     int programCount = sizeof(programName) / sizeof(programName[0]);
  345.     while (option)
  346.     {
  347.         system("cls");
  348.         for (i = 1; i < programCount; i++)
  349.         {
  350.             printf("%d - %s\n", i, programName[i]);
  351.         }
  352.         printf("0 - %s\n", programName[0]);
  353.         printf("\nWybor:");
  354.         scanf("%d", &option);
  355.         switch (option)
  356.         {
  357.         case 1:
  358.             printf("Podaj przyblizona wielkosc sortowanych tablicy:");
  359.             scanf("%d", &size);
  360.             printf("Podaj liczbe powtorzen:");
  361.             scanf("%d", &repeats);
  362.             compareSortingAlgorithms(size, repeats);
  363.             break;
  364.         case 2:
  365.             HeapSortHandle();
  366.             break;
  367.         case 3:
  368.             printf("Podaj wielkosc tablicy do posortowania:");
  369.             scanf("%d", &size);
  370.             printf("Podaj maksymalna wartosc k:");
  371.             scanf("%d", &k);
  372.             CountingSortHandle(size, k);
  373.             break;
  374.         case 0:
  375.             break;
  376.         }
  377.  
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement