The document provides an overview of sorting and searching in data structures, explaining sorting as the arrangement of data for easier searching. It details various sorting algorithms, including selection sort, bubble sort, insertion sort, shell sort, and non-comparison-based sorting techniques like radix and counting sort. Additionally, it covers searching algorithms such as binary search (iterative and recursive), Fibonacci search, and indexed sequential search.
The document provides an overview of sorting and searching in data structures, explaining sorting as the arrangement of data for easier searching. It details various sorting algorithms, including selection sort, bubble sort, insertion sort, shell sort, and non-comparison-based sorting techniques like radix and counting sort. Additionally, it covers searching algorithms such as binary search (iterative and recursive), Fibonacci search, and indexed sequential search.
What is Sorting • The arrangement of data in a preferred order is called sorting in the data structure. • By sorting data, it is easier to search through it quickly and easily. • The simplest example of sorting is a dictionary. Before the era of the Internet, when you wanted to look up a word in a dictionary, you would do so in alphabetical order. This made it easy. What is a Sorting Algorithm?
• A sorting algorithm is just a series of orders or instructions. In this, an
array is an input, on which the sorting algorithm performs operations to give out a sorted array. • Types of Sorting Techniques • There are various sorting algorithms are used in data structures. The following two types of sorting algorithms can be broadly classified: • Comparison-based: We compare the elements in a comparison-based sorting algorithm) • Non-comparison-based: We do not compare the elements in a non- comparison-based sorting algorithm) • Importance Of Sorting • When you are performing sorting on elements, many complications such as min/max, kth smallest/largest get automatically simplified. • Sorting also provides you with many algorithmic solutions, some of which might include divide and conquer, iterative, and recursive- based. • One of the biggest benefits of sorting in DSA is time complexity. The ultimate goal is to solve any kind of complex problem within the minimum amount of time. That is where different types of sorting in data structure come into play. It not only saves up your very precious time but also provides you with the right solution. Sorting Categories
• There are two different categories in sorting:
• Internal sorting: If the input data is such that it can be adjusted in the main memory at once, it is called internal sorting. • External sorting: If the input data is such that it cannot be adjusted in the memory entirely at once, it needs to be stored in a hard disk, floppy disk, or any other storage device. This is called external sorting. Selection Sort • Algorithm Pseudo Code
• Step 1:- Initialize i=0 for i in range(a[]):
• Step 2:- Repeat step 3 to 5 until min_id = i i<n for j in range(i + 1, a[]): • Step 3:- j= i+1 if a[min_id] >a[j]: • Step 4:- Repeat step 5 until j < n min_id = j Step 5:- if a[min_id] >a[j] a[i], a[min_id] = min_id = j a[min_id], a[i] a[i], a[min_id] = a[min_id], a[i] • Step 6:- stop Bubble Sort • Algorithm Pseudo Code in C++ • Step 1:- Initialize i=0 • Step 2:- Repeat step 3 to 5 For (i=0; i<n; i++) until i<n For ( j=0; j<n-i-1; j++) • Step :- j=0 { If If A[j]>a[j+1] then • Step 4:- Repeat step 5 until j < { n-i-1 temp= a[j] • Step 5:- If A[j]>a[j+1] then A[j]=a[j+1] temp= a[j] A[j+1]=a[j] A[j]=a[j+1] } } A[j+1]=a[j] • Step 6:- stop Insertion Sort • Algorithm Pseudo Code • Step 1:- Initialize i=1 for i in range(1,len(arr)): • Step 2:- Repeat step 3 to 5 temp = arr[i] until i<n j=i-1 • Step 3:- temp=a[i] ; j= I -1 while (j >= 0) & (arr[j] > • Step 4:- Repeat step 5 until temp): j > = 0 and a[j]>temp arr[j + 1] = arr[j] Step 5:- a[j+1] = a[j] j=j-1 j = j -1 arr[j + 1] = temp a[j+1] = temp Step 6:- stop Shell Sort • d = n // 2 • while d > 0: • for i in range(d, n): • temp = arr[i] • j=i • while j >= d and arr[j - d] > temp: • arr[j] = arr[j - d] • j -= d • arr[j] = temp • d = d // 2 Non Comparision Based Sorting • Radix Sort Algorithm • The key idea behind Radix Sort is to exploit the concept of place value. It assumes that sorting numbers digit by digit will eventually result in a fully sorted list. Radix Sort can be performed using different variations, such as Least Significant Digit (LSD) Radix Sort or Most Significant Digit (MSD) Radix Sort. • Step 1: Find the largest element in the array, If it has three digits, so we will iterate three times, once for each significant place. • Step 2: Sort the elements based on the unit place digits (X=0). We use a stable sorting technique, such as counting sort, to sort the digits at each significant place. • Step 3: Sort the elements based on the tens place digits. • Step 4: Sort the elements based on the hundreds place digits. Counting Sort • countingSort(array, n) // 'n' is the size of array • max = find maximum element in the given array • create count array with size maximum + 1 • Initialize count array with all 0's • for i = 0 to n • find the count of every unique element and • store that count at ith position in the count array • for j = 1 to max • Now, find the cumulative sum and store it in count array • for i = n to 1 • Restore the array elements • Decrease the count of every restored element by 1 • end countingSort Binary Search Iterative Low = 0 High = n - 1 while Low <= High: mid = (Low + High) // 2 if A[mid][0] < x: Low = mid + 1 if A[mid][0] > x: High = mid - 1 if A[mid][0] == x: return mid return -1 Binary Search Recursive if Low < High: mid = (High + Low) // 2 if A[mid][0] > x: return Search.binary_search_recursive(A, x, mid, Low) if A[mid][0] < x: return Search.binary_search_recursive(A, x, High, mid) else: return mid return -1 Fibonacci Search Step 1:- Find F(k) (i.e. kth Fibonacci no) which is greater than or equal to n Step 2:- If F(k)=0; then stop and print message "no not found" Step 3:- Offset = -1 Step 4:- i= min( offset + F(k-2), n-1) Step 5:- If s==a[i]; return i and stop the search If s > a[i]; k=k-1, offset=i and repeat step 4,5 If s<a[i] ; k=k-2 , offset = i and repeat step 4,5 Indexed Sequential Search Step 1 : Read the search element from user Step 2 : Divide the array into group according to the group size Step 3 : Create index array that contains starting index of the group Step 4 : If the group is present and the first element of that group is less than or equal to key element go to next group Else apply linear search in previous group