Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SortingAlgorithm.cpp : This file contains the 'main' function. Program execution begins and ends there.
- #include <iostream>
- void swap(int& el1, int& el2)
- {
- int temp = el1;
- el1 = el2;
- el2 = temp;
- }
- void bubbleSort(int arr[], int arrSize)
- {
- // Scan all the entire list/array
- for (int cursor = 0; cursor < arrSize; cursor++)
- // Iterate from i to i+1 position
- for (int i = 0; i < arrSize - cursor - 1; i++)
- // compare both element
- // if left > right
- if (arr[i] > arr[i + 1])
- // then swap the element to correct position
- swap(arr[i], arr[i+1]);
- }
- void selectionSort(int arr[], int arrSize)
- {
- for (int i = 0; i < arrSize - 1; i++)
- {
- int currMin = i;
- for (int currItem = i + 1; currItem < arrSize; currItem++)
- if (arr[currMin] > arr[currItem])
- currMin = currItem;
- swap(arr[currMin], arr[i]);
- }
- }
- void printArr(int arr[], int arrSize)
- {
- for (int i = 0; i < arrSize; i++)
- std::cout << arr[i] << " ";
- std::cout << "\n\n";
- }
- int main()
- {
- int arr[] = { 10, 11, 1, -2, 5 };
- int size = sizeof(arr) / sizeof(arr[1]);
- // bubbleSort(arr, size);
- selectionSort(arr, size);
- std::cout << "Sorted data: ";
- printArr(arr, size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement