0% found this document useful (0 votes)
16 views2 pages

Sorting

The document contains C++ code implementing three sorting algorithms: insertion sort, selection sort, and bubble sort. Each sorting function sorts an array of ten integers and counts the number of passes made during the sorting process. After sorting, the results and the number of passes are printed to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Sorting

The document contains C++ code implementing three sorting algorithms: insertion sort, selection sort, and bubble sort. Each sorting function sorts an array of ten integers and counts the number of passes made during the sorting process. After sorting, the results and the number of passes are printed to the console.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <iostream>

using namespace std;

void insertionsort(int a[])


{
int temp, pass=0;

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


{
for (int j = i; j >= 1; j--)
{
if (a[j]<a[j - 1])
{
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
pass ++;
}
}
}

cout << "After Insertion sort: " << endl;


for (int i = 0; i < 10; i++)
{
cout << a[i] << endl;
}
cout << "Number of passes: " << pass << endl;
}
void selectionsort(int ar[])
{
int temp, pass = 0;

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


{
int min = ar[i];
int loc = i;
for (int j = i + 1; j<10; j++)
{
if (min>ar[j])
{
min = ar[j];
loc = j;
}
}
temp = ar[i];
ar[i] = ar[loc];
ar[loc] = temp;
pass ++;
}
cout << "After Selection sort: " << endl;
for (int i = 0; i < 10; i++)
{
cout << ar[i] << endl;
}
cout << "Number of passes: " << pass << endl;
}
void bubblesort(int arr[])
{
int temp, pass =0;

for (int j = 1; j < 10; j++)


{
for (int i = 0; i< 10-j; i++)
{
if (arr[i]>arr[i + 1])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
pass ++;
}
}
}

cout << "After Bubble sort: " << endl;

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


{
cout <<arr[i] << endl;
}
cout << "Number of passes: " << pass << endl;
}

You might also like