Subject Name Subject Code Topic Name Mentor Name Department Group Member'S Name &roll No. (Contribution)
The document provides an overview of sorting algorithms, including definitions, types, and complexities associated with sorting in data structures and analysis. It covers various sorting methods such as Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Shell Sort, detailing their algorithms and complexities. Additionally, it discusses the concepts of stability in sorting and provides references for further reading.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
22 views10 pages
Subject Name Subject Code Topic Name Mentor Name Department Group Member'S Name &roll No. (Contribution)
The document provides an overview of sorting algorithms, including definitions, types, and complexities associated with sorting in data structures and analysis. It covers various sorting methods such as Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Shell Sort, detailing their algorithms and complexities. Additionally, it discusses the concepts of stability in sorting and provides references for further reading.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10
SUBJECT NAME: DATA STRUCTURE & ANALYSIS
SUBJECT CODE: PCC CS301
TOPIC NAME: SORTING
MENTOR NAME: MONU BHAGAT
DEPARTMENT: CSE IOT
GROUP MEMBER’S NAME &ROLL NO.:
(contribution) 1)ANNASHREE HUI 13031423007 (Selection sort) 2)ANURIMA SARKAR 13031423009 (ppt, introduction, shell sort) 3)SHARONYA BANERJEE 13031423049 (complexity, quick sort) 4)SOUMILI MITRA 13031423052 (bubble sort) 5)SRIBIDYA SENGUPTA 13031423055 (stability, insertion sort) WHAT IS SORTING ? INTRODUCTION Sorting refers to the process of rearrangement of the elements of an array so that they can be placed in a particular order. In DSA, the term sorting is associated with rearrangement in ascending order. For example, in an array A = {A1, A2, A3, A4, A5 }, the array is called to be sorted in ascending order if element of A are arranged like A1 < A2 <A3 < A4 <A5. TYPES OF SORTING IN DSA: Based on the types of algorithm used sorting can be mainly classified into the following types: 1)Bubble sort 2)Insertion Sort 3)Selection Sort 4)Quick Sort 5)Bubble Sort 6)Merge Sort COMPLEXITY It is of two types 1)Time Complexity is defined as the number of times a particular instruction set is executed rather than the total time taken. 2)Space Complexity is the total memory space required by the program for its execution Types of Time Complexity : • Best Time Complexity: Define the input for which the algorithm takes less time or minimum time. Average Time Complexity: In the average case take all random inputs and calculate the computation time for all inputs A Table with space and time Worst Time Complexity: Define the input for complexity of the described sorting which algorithm takes a long time or maximum algorithms time. STABILITY
Stability is a property of sorting
algorithms that determines how they handle elements with equal values. Stable Sorting: Preserves the relative order of equal elements after sorting. Unstable Sorting: May change the relative order of equal elements. A sorting algorithm is said to be stable if it preserves the relative order of equal elements in the input sequence. Algorithm BUBBLE SORT Bubble Sort gets its name because of the way Step 1- START Step 2: If the list contains only one element, it is already sorted. Return the list. larger elements "bubble" to the top (end) of the list Step 3: Pick the next element in the list. Step through successive swaps with adjacent elements. 4: Compare the current element with the adjacent element. • Bubble Sort gets its name because of the way Step 5: If the current element is greater than larger elements "bubble" to the top (end) of the the adjacent element, swap them. Step 6: list through successive swaps with adjacent Repeat the comparison and swapping elements. process for the entire list, ensuring each pass place the next largest element in its correct • For instance, within an array, pairs of adjacent position. Step 7: Repeat the entire process until the list elements are compared, and if the preceding is sorted element is larger than the following one, a swap occurs. This process causes the largest element CODE- (int array[], int n) { to move to the end of the array. The same steps int i, j, temp; for (i = 0; i < n-1; i++) { are repeated for the remaining elements until for (j = 0; j < n-i-1; j++) { the entire array is sorted. if (array[j] > array[j+1]) { // Swap array[j] and array[j+1] • Bubble Sort is not ideal for handling large data temp = array[j]; resources, due to its average nature. Its array[j] = array[j+1]; worstcase time complexity is O(n²), where n is array[j+1] = temp; } } } } the number of items in the array. INSERTION SORT • This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. Algorithm Step 1 − If it is the first element, it is already • For example, the lower part of an array is sorted. return 1; maintained to be sorted. An element which Step 2 − Pick next element is to be 'inserted' in this sorted sub-list, has Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift all the elements to find its appropriate place and then it has in the sorted sub-list that is greater than the to be inserted there. Hence the name, value to be sorted insertion sort. Step 5 − Insert the value Step 6 − Repeat until list is sorted • The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). Code in a Function void insertionSort(int array[], int size){ • This algorithm is not suitable for large data int key, j; for(int i = 1; i 0 && array[j-1]>key) { sets as its average and worst case array[j] = array[j-1]; j--; } complexity are of Ο(n 2), where n is the array[j] = key; //insert in right place number of items. }} • If the given numbers are sorted, this algorithm runs in O(n) time. If the given numbers are in reverse order, the algorithm SELECTION SORT SELECTION SORT(arr, n) Step 1: Repeat Steps 2 and 3 for I = 0 to n-1 In selection sort, the smallest value Step 2: CALL SMALLEST(arr, I, n, among the unsorted elements of the pos) array is selected in every pass and Step 3: SWAP arr[i] with arr[pos] inserted to its appropriate position into [END OF LOOP] the array. In this algorithm, the array is Step 4: EXIT divided into two parts, first is sorted SMALLEST (arr, I, n, pos) part, and another one is the unsorted Step 1: [INITIALIZE] SET SMALL part. Initially, the sorted part of the = arr[i] array is empty, and unsorted part is the Step 2: [INITIALIZE] SET pos = I given array. Sorted part is placed at the Step 3: Repeat for j = i+1 to n left, while the unsorted part is placed at the right. If (SMALL > arr[j]) SET SMALL = arr[j] Then the first smallest element is SET pos = j selected from the unsorted array and [END OF if] placed at the first position. After that QUICK SORT CODE PICTORIAL REPRESENTATION What is Quick sort? • Quick sort is a sorting algorithm. It works by selecting a pivot element and arranges the other values greater or less than it on either sides.It is not very stable. • Helps to sort big Data sets SHELL SORT It is a sorting algorithm that is an ShellSort(a, n) extended version of insertion sort. // 'a' is the given array, 'n' is the size of In insertion sort, at a time, elements array can be moved ahead by one position only which increase the algorithm's for (interval = n/2; interval > 0; interval /= execution time. But shell sort 2) overcomes this drawback of insertion for ( i = interval; i < n; i+= 1) temp = a[i]; sort. It allows the movement and for(j=i;j>=interval&&a[j-interval]> temp; j -= swapping of far-away elements as well. interval) This algorithm first sorts the elements a[j] = a[j - interval]; that are far away from each other, then it subsequently reduces the gap a[j] = temp; between them. This gap is called as interval Time complexity of shell sort is: 1. Best case - O(n*logn) (when the elements are in ascending order & no change is required) 2. Average Case - O(n*log(n) ) (when 2
elements are neither in ascending or
descending order 3. Worst case - O(n ) (when elements are 2 for interval n/2=4 for interval n/2=2 for interval n/2=1 in descending order REFERENCE WEBSITES : • https://www.javatpoint.com/sorting- algorithms • https://www.geeksforgeeks.org/sorting- algorithms/ • https://www.programiz.com/dsa/sorting- algorithm BOOKS : • Data Structures Using C 2E : Reema Thareja • Programming in C : E.Balaguruswamy