Write a function for Selection Sort, which uses while loop(s) to complete its procedure. Call the function in main for demonstration. You can initialize array with random values.

 Write a function for Selection Sort, which uses while loop(s) to complete its procedure. Call the function in main for demonstration. You can initialize array with random values.


#include<iostream>

#include<ctime>

using namespace std;

void Display_Data(int Array[],int size)

{

for(int i=0;i<size;i++)

{

cout<<Array[i]<<"  ";

}

}

void Swap_Data(int *a,int *b)

{

int temp = *a;

*a = *b;

*b = temp;

}

void Selection_Sort(int Array[],int size)

{

for(int i=0;i<size-1;i++)

{

int min=i;

for(int j=i+1;j<size;j++)

{

if(Array[j]<Array[min])

{

min=j;

}

}

Swap_Data(&Array[i],&Array[min]);

}

}

int main()

{

int size;

    cout<< "How Big Do You Want The Array" << endl;

    cin >> size;

int Array[size];

srand((unsigned)time(0));

for(int i=0; i<size; i++)

{

        Array[i] = (rand()%1000)+1;

    }

cout<<"-----BEFORE SORTING----"<<endl;

cout<<endl;

Display_Data(Array,size);

cout<<"\n\n-----AFTER SORTING----"<<endl;

cout<<endl;

Selection_Sort(Array,size);

Display_Data  (Array,size);

    return 0;

}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.