Aim: Algorithm (If Any)
Aim: Algorithm (If Any)
Aim:
===== Aim Here ====
Algorithm (if any):
======================
===== Algorithm Here ====
======================
Code:
======================
===== Program Here =====
======================
Output:
===== Screenshot =====
//implementation of Bubble Sort technique in C++.
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp;
int a[5] = {10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++) {
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5-1; i++) {
for(j = 0; j<5-1-i; j++)
{
if(a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<5; i++) {
cout <<a[i]<<"\t";
}
return 0;
}
//implementation of Insertion Sorting technique in C++.
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp;
int a[5] = {10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
#include <iostream>
using namespace std;
void merge(int *,int, int , int );
#include<iostream>
using namespace std;
for(int i=0;i<5;i++)
{
pos = findSmallest (myarray,i);
temp = myarray[i];
myarray[i]=myarray[pos];
myarray[pos] = temp;
}
#include <iostream>
using namespace std;
// Swap two elements - Utility function
int pivot;
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
// partition the array using last element as pivot
int partition (int arr[], int low, int high)
{
int i = (low - 1);
for (int j = low; j <= high- 1; j++)
{
//if current element is smaller than pivot, increment the low element
//swap elements at i and j
if (arr[j] <= pivot)
{
i++; // increment index of smaller element
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
//quicksort algorithm
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
//partition the array
int pivot = partition(arr, low, high);
//sort the sub arrays independently
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
}
void displayArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<"\t";
}
int main()
{
int arr[] = {12,23,3,43,51};
int n = sizeof(arr)/sizeof(arr[0]);
cout<<"Input array"<<endl;
displayArray(arr,n);
cout<<endl;
quickSort(arr, 0, n-1);
cout<<"Array sorted with quick sort"<<endl;
displayArray(arr,n);
return 0;
}