sorting


PHOTO EMBED

Wed Dec 09 2020 03:00:16 GMT+0000 (UTC)

Saved by @mahmoud hussein #c++

 
#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;
            }
        
    }
}
content_copyCOPY

http://cpp.sh/