Srting Algos

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Minhaj

University School of Computer Science


Lahore

Assignment -4
Data Structure & Algorithms
Submitted by: M.Umer Farooq Roll no:77
Submitted to: Mam Unaiza Rehman Section: B
Class: BSCS Semester: 3rd
Question-1:
Write the code of the following sorting algorithms.
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Quick Sort
5. Merge Sort
Bubble Sort:
#include<iostream>
using namespace std;
int main(){
int array[10]={10,5,4,3,2,5,7,8,9,10};
int count=1;
while(count<10){
for(int i=0;i<10-count;i++){
if(array[i]<array[i+1]){
int temp=array[i+1];
array[i+1]=array[i];
array[i]=temp;
}
}
count++;
}
for(int i=0;i<10;i++){
cout<<array[i];
}
}
Selection Sort:
#include<iostream>
using namespace std;
int main(){
int array[5]={5,4,1,2,6};
for(int i=0;i<5;i++){
for(int j=i+1;j<4;j++){
if(array[j]<array[i]){
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
for(int i=0;i<5;i++){
cout<<array[i];
}
}
Insertion Sort:
#include<iostream>
using namespace std;
int main(){
int array[10]={10,5,4,3,2,5,7,8,9,10};
for(int i=1;i<10;i++){
int temp=array[i];
int j=i-1;
while(array[j]< temp&&j>=0){
array[j+1]=array[j];
j--;
}
array[j+1]=temp;
}
for(int i=0;i<10;i++){
cout<<array[i];
}
}
Quick Sort:
#include <iostream>
using namespace std;

int partition(int arr[], int start, int end)


{

int pivot = arr[start];

int count = 0;
for (int i = start + 1; i <= end; i++) {
if (arr[i] <= pivot)
count++;
}
int pivotIndex = start + count;
swap(arr[pivotIndex], arr[start]);
int i = start, j = end;

while (i < pivotIndex && j > pivotIndex) {

while (arr[i] <= pivot) {


i++;
}

while (arr[j] > pivot) {


j--;
}

if (i < pivotIndex && j > pivotIndex) {


swap(arr[i++], arr[j--]);
}
}

return pivotIndex;
}

void quickSort(int arr[], int start, int end)


{
if (start >= end)
return;
int p = partition(arr, start, end);
quickSort(arr, start, p - 1);
quickSort(arr, p + 1, end);
}

int main()
{
int arr[] = { 9, 3, 4, 2, 1, 8 };
int n = 6;
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Merge Sort:
#include <iostream>
using namespace std;
void merge(int array[], int const left, int const mid,
int const right)
{
auto const subArrayOne = mid - left + 1;
auto const subArrayTwo = right - mid;

auto *leftArray = new int[subArrayOne],


*rightArray = new int[subArrayTwo];

for (auto i = 0; i < subArrayOne; i++)


leftArray[i] = array[left + i];
for (auto j = 0; j < subArrayTwo; j++)
rightArray[j] = array[mid + 1 + j];

auto indexOfSubArrayOne
= 0; // Initial index of first sub-array
indexOfSubArrayTwo
= 0; // Initial index of second sub-array
int indexOfMergedArray
= left; // Initial index of merged array

while (indexOfSubArrayOne < subArrayOne


&& indexOfSubArrayTwo < subArrayTwo) {
if (leftArray[indexOfSubArrayOne]
<= rightArray[indexOfSubArrayTwo]) {
array[indexOfMergedArray]
= leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
}
else {
array[indexOfMergedArray]
= rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
}
indexOfMergedArray++;
}

while (indexOfSubArrayOne < subArrayOne) {


array[indexOfMergedArray]
= leftArray[indexOfSubArrayOne];
indexOfSubArrayOne++;
indexOfMergedArray++;
}
while (indexOfSubArrayTwo < subArrayTwo) {
array[indexOfMergedArray]
= rightArray[indexOfSubArrayTwo];
indexOfSubArrayTwo++;
indexOfMergedArray++;
}
delete[] leftArray;
delete[] rightArray;
}

void mergeSort(int array[], int const begin, int const end)


{
if (begin >= end)
return;

auto mid = begin + (end - begin) / 2;


mergeSort(array, begin, mid);
mergeSort(array, mid + 1, end);
merge(array, begin, mid, end);
}
void printArray(int A[], int size)
{
for (auto i = 0; i < size; i++)
cout << A[i] << " ";
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
auto arr_size = sizeof(arr) / sizeof(arr[0]);

cout << "Given array is \n";


printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

cout << "\nSorted array is \n";


printArray(arr, arr_size);
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