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

Aim: Algorithm (If Any)

This document contains implementations of several sorting algorithms in C++, including bubble sort, insertion sort, merge sort, selection sort, and quicksort. For each algorithm, it includes the code, inputs/outputs, and a brief description of the approach.

Uploaded by

Azeem Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Aim: Algorithm (If Any)

This document contains implementations of several sorting algorithms in C++, including bubble sort, insertion sort, merge sort, selection sort, and quicksort. For each algorithm, it includes the code, inputs/outputs, and a brief description of the approach.

Uploaded by

Azeem Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Format of Program

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;

for(i = 1; i<5; i++)


{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
return 0;
}
//Given below is an implementation of merge sort technique using C++.

#include <iostream>
using namespace std;
void merge(int *,int, int , int );

void merge_sort(int *arr, int low, int high)


{
int mid;
if (low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
//merge or conquer sorted arrays
merge(arr,low,high,mid);
}
}
// Merge sort
void merge(int *arr, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
}
else {
c[k] = arr[j];
k++;
j++;
}
}
if(i>mid)
{

while (j <= high)


{
c[k] = arr[j];
k++;
j++;
}
}
else
{

while (i <= mid) {


c[k] = arr[i];
k++;
i++;
}
}
for (i = low; i < k; i++) {
arr[i] = c[i];
}
}
// read input array and call mergesort
int main()
{
int myarray[30], num;
cout<<"Enter number of elements to be sorted:";
cin>>num;
cout<<"Enter "<<num<<" elements to be sorted:";
for (int i = 0; i < num; i++)
{
cin>>myarray[i];
}
merge_sort(myarray, 0, num-1);
cout<<"Sorted array\n";
for (int i = 0; i < num; i++)
{
cout<<myarray[i]<<"\t";
}
}

//implementation of Selection Sort using C++.

#include<iostream>
using namespace std;

int findSmallest (int[],int);


int main ()
{
int myarray[5] = {12,45,8,15,33};
int pos,temp;
cout<<"\n Input list of elements to be Sorted\n";
for(int i=0;i<5;i++)
{
cout<<myarray[i]<<"\t";
}

for(int i=0;i<5;i++)
{
pos = findSmallest (myarray,i);
temp = myarray[i];
myarray[i]=myarray[pos];
myarray[pos] = temp;
}

cout<<"\n Sorted list of elements is\n";


for(int i=0;i<5;i++)
{
cout<<myarray[i]<<"\t";
}
return 0;
}

int findSmallest(int myarray[],int i)


{
int ele_small,position,j;
ele_small = myarray[i];
position = i;
for(j=i+1;j<5;j++)
{
if(myarray[j]<ele_small)
{
ele_small = myarray[j];
position=j;
}
}
return position;
}

//Implementation of Quick Sort technique using C++.

#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;
}

You might also like