0% found this document useful (0 votes)
2 views

Bubble Sort

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)
2 views

Bubble Sort

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
You are on page 1/ 2

#include <iostream>

using namespace std;


void bubblesort(int arr[], int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
}
int main()
{
int arr[40];
int n;
cout << "Enter the no of elements in array" << endl;
cin >> n;
cout << "Enter the elements" << endl;
for (int j = 0; j < n; j++)
cin >> arr[j];
bubblesort(arr, n);
cout << "Sorted Array" << endl;
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
#include <iostream>
using namespace std;
void insertion_sort(int A[], int n)
{
int key, j;
for (int i = 1; i < n; i++)
{
key = A[i];
j = i - 1;
while (j >= 0 && A[j] > key)
{
A[j+1] = A[j];
j--;
}
A[j + 1] = key;
}
}
int main()
{
int num;
int arr[20];
cout << "Enter the number of elements in the array : ";
cin >> num;
for (int i = 0; i < num; i++)
{
cout << "Enter the element of the array : ";
cin >> arr[i];
}
cout << "Unsorted Array :" << endl;
for (int i = 0; i < num; i++)
cout << arr[i] << " ";
cout << endl;
insertion_sort(arr, num);
cout << "Sorted Array :" << endl;
for (int i = 0; i < num; i++)
cout << arr[i] << " ";
return 0;
}

You might also like