Write a function for Insertion 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 Insertion 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 Insertion_Sort(int Array[],int size)

{

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

{

int min=Array[i];

int j=i-1;

while(min<Array[j]&&j>=0)

{

Array[j+1]=Array[j];

--j;

}

Array[j+1]=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;

Insertion_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.