#include<iostream> #include <fstream> using namespace std; void selectionSort(double list[], int listSize); int main() { double list[] = { 3.4, 5, 3, 3.5, 2.2, 1.9, 2 }; selectionSort(list, 7); for (int i = 0; i < 7; i++) { cout << list[i]<<" "; } } //decreasing order void selectionSort(double list[], int listSize) { for (int i = listSize-1; i >= 0 ; i--) { double currentMax = list[i]; int currentMaxIndex = i; for (int j = i - 1; j >= 0; j--) { if (currentMax < list[j]) { currentMax = list[j]; currentMaxIndex = j; } } if (currentMaxIndex != i) { list[currentMaxIndex] = list[i]; list[i] = currentMax; } } } //increasing order void selectionSort(double list[], int listSize) { for (int i = 0; i < listSize - 1; i++) { double currentMin = list[i]; int currentMinIndex = i; for (int j = i + 1; j < listSize; j++) { if (currentMin > list[j]) { currentMin = list[j]; currentMinIndex = j; } } if (currentMinIndex != i) { list[currentMinIndex] = list[i]; list[i] = currentMin; } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter