Guest User

Prebublávanie-Bubble-Sort_2014-15

a guest
Dec 24th, 2014
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. const int MAX_RANDOM = 999; /* ? obmedzené len na 19 čísel (prvkov ?  oki, zmenené na 99, al. 999 */
  9.  
  10. void fillWithRandom(int array[], const int arrayLength) {
  11.     for (int i = 0; i < arrayLength; ++i) {
  12.         array[i] = rand() % (MAX_RANDOM + 1);
  13.     }
  14. }
  15.  
  16. void swap(int array[], const int index1, const int index2) {
  17.      int tmp = array[index1];
  18.      array[index1] = array[index2];
  19.      array[index2] = tmp;
  20. }
  21.  
  22. void bubbleSort(int array[], const int arrayLength, int &comparisonCount,
  23.                 int &swapCount, const bool vzestupne) {
  24.      
  25.     for (int i = 0; i < arrayLength - 1; ++i) {
  26.         for (int j = 0; j < arrayLength - i - 1; ++j) {
  27.             ++comparisonCount;
  28.             if (((vzestupne == true) && (array[j+1] < array[j]))
  29.              || ((vzestupne == false) && (array[j+1] > array[j]))) {
  30.                 swap(array, j, j + 1);
  31.                 ++swapCount;
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. void printOut(int array[], const int arrayLength) {
  38.     for (int i = 0; i < arrayLength; ++i) {
  39.         std::cout << array[i] << ' ';
  40.     }
  41. }
  42.  
  43. void sortAndPrintOut(int array[], const int arrayLength, const bool vzestupne){
  44.     int comparisonCount = 0;
  45.     int swapCount = 0;
  46.    
  47.     std::cout << "\nPuvodni pole:   ";
  48.     printOut(array, arrayLength);
  49.    
  50.     bubbleSort(array, arrayLength, comparisonCount, swapCount, vzestupne);
  51.  
  52.     std::cout << "\nSetridene pole: ";
  53.     printOut(array, arrayLength);
  54.     printf("\npocet porovnani: %d \npocet swapu: %d\n", comparisonCount, swapCount);
  55. }
  56.  
  57. int* createArrayOfRandomNumbersAndSortAndPrintOut(const bool vzestupne) {
  58.     printf("\nZadaj pocet prvkov (cisel): ");
  59.     int numberCount;
  60.     scanf("%d", &numberCount);
  61.     printf("\n");
  62.    
  63.     int numbers[numberCount];
  64.     fillWithRandom(numbers, numberCount);
  65.     sortAndPrintOut(numbers, numberCount, vzestupne);
  66. }
  67.  
  68. int main() {
  69.     int cisla[] = { 5, 1, 3, 8, 6 };
  70.     sortAndPrintOut(cisla, 5, true);
  71.    
  72.     int cisla2[] = { 1, 5, 6, 3, 9 };
  73.     sortAndPrintOut(cisla2, 5, false);
  74.    
  75.     int numberCount3 = 12;
  76.     int cisla3[numberCount3];
  77.     fillWithRandom(cisla3, numberCount3);
  78.     sortAndPrintOut(cisla3, numberCount3, true);
  79.    
  80.     int numberCount4 = 16;
  81.     int cisla4[numberCount4];
  82.     fillWithRandom(cisla4, numberCount4);
  83.     sortAndPrintOut(cisla4, numberCount4, false);
  84.    
  85.     createArrayOfRandomNumbersAndSortAndPrintOut(true);
  86.     createArrayOfRandomNumbersAndSortAndPrintOut(false);
  87.    
  88.     getch();
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment