Bubble Sort

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy