Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sorting Algorithms: Bubble, Quick, and Merge Sort, Cheat Sheet of Data Structures and Algorithms

An in-depth exploration of three popular sorting algorithms: bubble sort, quick sort, and merge sort. It delves into the theoretical foundations of each algorithm, including their time complexities, and presents a user-choice program that allows the user to sort an array using any of the three methods. The program includes detailed code implementation for each sorting algorithm, showcasing the practical application of these fundamental concepts. This document serves as a valuable resource for students and professionals interested in understanding and implementing efficient sorting techniques, which are crucial in data structures and algorithm design.

Typology: Cheat Sheet

2021/2022

Uploaded on 08/06/2024

vedant-raje
vedant-raje 🇮🇳

1 document

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
DSA ASSIGNMENT
DURVESH GAWDE S1_35
AIM: Write a user choice program which to sort elements of array, asked from user, by
bubble, quick or merge sort.
1) BUBBLE SORT
THEORY: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in the wrong order. This algorithm is not suitable for large
data sets as its time complexity is quite high.
TIME COMPLEXITY: O(n^2)
2) QUICK SORT
THEORY: Quick Sort is a sorting algorithm based picking an element as a pivot and partitions
the given array around the picked pivot by placing the pivot in its correct position in the
sorted array.
TIME COMPLEXITY: O(n log n)
3)MERGE SORT
THEORY: Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It
works by recursively dividing the input array into smaller subarrays and sorting those
subarrays then merging them back together to obtain the sorted array. In simple terms, we
can say that the process of merge sort is to divide the array into two halves, sort each half,
and then merge the sorted halves back together. This process is repeated until the entire
array is sorted.
TIME COMPLEXITY: O(n log n)
CODE:
#include<stdio.h>
int mergeSort(int a[], int n)
{
if(n<2)
return a;
int mid = n/2;
int a_left [mid];
int a_right [n-mid];
for(int i=0; i<=mid-1; i++)
pf3
pf4
pf5

Partial preview of the text

Download Sorting Algorithms: Bubble, Quick, and Merge Sort and more Cheat Sheet Data Structures and Algorithms in PDF only on Docsity!

DSA ASSIGNMENT

DURVESH GAWDE S1_ AIM: Write a user choice program which to sort elements of array, asked from user, by bubble, quick or merge sort. 1 ) BUBBLE SORT THEORY: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its time complexity is quite high. TIME COMPLEXITY: O(n^2) 2 ) QUICK SORT THEORY: Quick Sort is a sorting algorithm based picking an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. TIME COMPLEXITY: O(n log n) 3 )MERGE SORT THEORY: Merge sort is a sorting algorithm that follows the divide-and-conquer approach. It works by recursively dividing the input array into smaller subarrays and sorting those subarrays then merging them back together to obtain the sorted array. In simple terms, we can say that the process of merge sort is to divide the array into two halves, sort each half, and then merge the sorted halves back together. This process is repeated until the entire array is sorted. TIME COMPLEXITY: O(n log n) CODE: #include<stdio.h> int mergeSort(int a[], int n) { if(n< 2 ) return a; int mid = n/ 2 ; int a_left [mid]; int a_right [n-mid]; for(int i= 0 ; i<=mid- 1 ; i++)

a_left[i] = a[i]; } int ln = sizeof(a_left) / sizeof(a_left[ 0 ]); for(int i=mid; i<=n- 1 ; i++) { a_right[i-mid] = a[i]; } int rn = sizeof(a_right) / sizeof(a_right[ 0 ]); mergeSort(a_left, ln); mergeSort(a_right, rn); merge(a_left, ln , a_right, rn, a); return a; } void merge(int a1[], int l, int a2[], int r, int aMID[]) { int i= 0 , j= 0 , k= 0 ; while(i<l && j<r) { if(a1[i] <= a2[j]) { aMID[k] = a1[i]; i = i+ 1 ; } else { aMID[k] = a2[j]; j = j+ 1 ; }

int temp = aray[pIndex]; aray[pIndex] =aray[end]; aray[end] = temp; return pIndex; } int quickSort(int a[], int beg, int end) { if(beg<end) { int pIndex = partition(a, beg, end); quickSort(a, beg, pIndex- 1 ); quickSort(a, pIndex+ 1 , end); } return a; } int bubbleSort(int a[], int n) { for(int i= 0 ; i<n- 1 ; i++) { for(int j= 0 ; j<n- 1 ; j++) { if(a[j] > a[j+ 1 ]) { int temp = a[j]; a[j] = a[j+ 1 ]; a[j+ 1 ] = temp; } } }

return a; } void main() { int num,choice; printf("Enter the number of elements: "); scanf("%d", &num); int a[num]; for(int i= 0 ; i<num; i++) { printf("Enter the element: "); scanf("%d", &a[i]); } printf("\n1_Bubble Sort\n2_Quick Sort\n3_Merge Sort\n4_Exit\nEnter your choice:"); scanf("%d", &choice); int f; int len = sizeof(a) / sizeof(a[ 0 ]); switch(choice) { case 1 : f = bubbleSort(a, num); for( int i= 0 ; i<num; i++){ printf("%d ", a[i]); } break; case 2 : f = quickSort(a, 0 , num- 1 ); for(int i= 0 ; i<num; i++){

2 _Quick Sort 3 _Merge Sort 4 _Exit Enter your choice: 3 22 44 59 66 99 1 _Bubble Sort 2 _Quick Sort 3 _Merge Sort 4 _Exit Enter your choice: 4