Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- using namespace std;
- const int MAX_RANDOM = 999; /* ? obmedzené len na 19 čísel (prvkov ? oki, zmenené na 99, al. 999 */
- void fillWithRandom(int array[], const int arrayLength) {
- for (int i = 0; i < arrayLength; ++i) {
- array[i] = rand() % (MAX_RANDOM + 1);
- }
- }
- void swap(int array[], const int index1, const int index2) {
- int tmp = array[index1];
- array[index1] = array[index2];
- array[index2] = tmp;
- }
- void bubbleSort(int array[], const int arrayLength, int &comparisonCount,
- int &swapCount, const bool vzestupne) {
- for (int i = 0; i < arrayLength - 1; ++i) {
- for (int j = 0; j < arrayLength - i - 1; ++j) {
- ++comparisonCount;
- if (((vzestupne == true) && (array[j+1] < array[j]))
- || ((vzestupne == false) && (array[j+1] > array[j]))) {
- swap(array, j, j + 1);
- ++swapCount;
- }
- }
- }
- }
- void printOut(int array[], const int arrayLength) {
- for (int i = 0; i < arrayLength; ++i) {
- std::cout << array[i] << ' ';
- }
- }
- void sortAndPrintOut(int array[], const int arrayLength, const bool vzestupne){
- int comparisonCount = 0;
- int swapCount = 0;
- std::cout << "\nPuvodni pole: ";
- printOut(array, arrayLength);
- bubbleSort(array, arrayLength, comparisonCount, swapCount, vzestupne);
- std::cout << "\nSetridene pole: ";
- printOut(array, arrayLength);
- printf("\npocet porovnani: %d \npocet swapu: %d\n", comparisonCount, swapCount);
- }
- int* createArrayOfRandomNumbersAndSortAndPrintOut(const bool vzestupne) {
- printf("\nZadaj pocet prvkov (cisel): ");
- int numberCount;
- scanf("%d", &numberCount);
- printf("\n");
- int numbers[numberCount];
- fillWithRandom(numbers, numberCount);
- sortAndPrintOut(numbers, numberCount, vzestupne);
- }
- int main() {
- int cisla[] = { 5, 1, 3, 8, 6 };
- sortAndPrintOut(cisla, 5, true);
- int cisla2[] = { 1, 5, 6, 3, 9 };
- sortAndPrintOut(cisla2, 5, false);
- int numberCount3 = 12;
- int cisla3[numberCount3];
- fillWithRandom(cisla3, numberCount3);
- sortAndPrintOut(cisla3, numberCount3, true);
- int numberCount4 = 16;
- int cisla4[numberCount4];
- fillWithRandom(cisla4, numberCount4);
- sortAndPrintOut(cisla4, numberCount4, false);
- createArrayOfRandomNumbersAndSortAndPrintOut(true);
- createArrayOfRandomNumbersAndSortAndPrintOut(false);
- getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment