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

Module 3 (Part 2- Sorting)

Uploaded by

pratapshivam007
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
5 views

Module 3 (Part 2- Sorting)

Uploaded by

pratapshivam007
Copyright
© © All Rights Reserved
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/ 14

Problem Solving using Data

Structures
Module 3
(Part 2: Sorting)
Sorting
• Sorting means arranging the elements of an array so that they are placed in some relevant
order which may be either ascending or descending.
• If A is an array, then the elements of A are arranged in a sorted order (ascending order) in
such a way that A[0] < A[1] < A[2] < ...... < A[N].
• For example, int A[] = {10,4,13,7,50,25}; Then the sorted array (ascending order) can be given
as: A[] = {4,7,10,13,25,50}.
• A sorting algorithm puts the elements of a list in a certain order, which can be either
numerical order, lexicographical order, or any user-defined order.
• Efficient sorting algorithms are widely used to optimize the use of other algorithms like
search and merge algorithms which require sorted lists to work correctly.
• There are two types of sorting:
– Internal sorting which deals with sorting the data stored in the computer’s memory.
– External sorting which deals with sorting the data stored in files. External sorting is
applied when there is voluminous data that cannot be stored in the memory.
• Sorting on Multiple Keys- Many a times, when performing real-world applications, it is
desired to sort arrays of records using multiple keys. This situation usually occurs when a
single key is not sufficient to uniquely identify a record. For example, in a big organization we
may want to sort a list of employees on the basis of their departments first and then
according to their names in alphabetical order.
Practical Considerations for Internal Sorting
• Records can be sorted either in ascending or descending order based on a
field often called as the sort key.
• The list of records can be either stored in a contiguous and randomly
accessible data structure like array or may be stored in a dispersed and
only sequentially accessible data structure like a linked list.
• Irrespective of the underlying data structure used to store the records, the
logic to sort the records will be same and only the implementation details
will differ.
• When analyzing the performance of different sorting algorithms, the
practical considerations would be the following:
– Number of sort key comparisons that will be performed
– Number of times the records in the list will be moved
– Best case performance
– Worst case performance
– Average case performance
– Stability of the sorting algorithm where stability means that equivalent
elements or records retain their relative positions even after sorting is done.
Bubble Sort
• Simplest sorting algorithm.
• Repeatedly swap the adjacent elements if they are in the wrong order.
• Not suitable for large data sets as its average and worst-case time
complexity are quite high.
• The array is sorted using multiple passes. After the first pass, the
maximum element goes to end (its correct position). Same way, after
second pass, the second largest element goes to second last position and
so on.
• After k passes, the largest k elements must have been moved to the last k
positions.
Algorithm
BUBBLE_SORT(A, N)
Step 1: Repeat Step 2 For I = 0 to N-1
Step 2: Repeat For J = 0 to N - I
Step 3: IF A[J] > A[J + 1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT
Complexity Analysis of Bubble Sort:
Time Complexity: O(n2)
Auxiliary Space: O(1)
Advantages of Bubble Sort:
1. Easy to understand and implement.
2. Does not require any additional memory space.
3. Stable sorting algorithm, meaning that elements with the same key
value maintain their relative order in the sorted output.
Disadvantages of Bubble Sort:
1. Time complexity is O(n2) which makes it very slow for large data
sets.
2. Almost no or limited real world applications. It is mostly used in
academics to teach different ways of sorting.
Insertion sort
• Simple sorting algorithm that works by iteratively inserting each element
of an unsorted list into its correct position in a sorted portion of the list.
Algorithm
INSERTION-SORT (ARR, N)
Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1
Step 2: SET TEMP = ARR[K]
Step 3: SET J = K - 1
Step 4: Repeat while TEMP <= ARR[J]
SET ARR[J + 1] = ARR[J]
SET J = J - 1
[END OF INNER LOOP]
Step 5: SET ARR[J + 1] = TEMP
[END OF LOOP]
Step 6: EXIT
Complexity Analysis of Insertion Sort
• Time Complexity
Best case: O(n), If the list is already sorted, where n is the number of
elements in the list.
Average case: O(n2), If the list is randomly ordered
Worst case: O(n2), If the list is in reverse order
• Space Complexity
Auxiliary Space: O(1), Insertion sort requires O(1) additional space, making it
a space-efficient sorting algorithm.
Advantages of Insertion Sort
1. An in-place sorting algorithm.
2. It is simple to implement which is great for small datasets.
3. Insertion sort is adaptive in nature, i.e. it is appropriate for data sets
which are already partially sorted.
4. Best-case scenario only requires O(n) time.
Selection Sort
• Comparison-based sorting algorithm.
• It sorts an array by repeatedly selecting the smallest (or largest) element
from the unsorted portion and swapping it with the first unsorted
element. This process continues until the entire array is sorted.
Algorithm

SMALLEST (ARR, K, N, POS) SELECTION SORT(ARR, N)


Step 1: [INITIALIZE] SET SMALL = ARR[K] Step 1: Repeat Steps 2 and 3 for K = 1
Step 2: [INITIALIZE] SET POS = K to N-1
Step 3: Repeat for J = K+1 to N-1 Step 2: CALL SMALLEST(ARR, K, N, POS)
IF SMALL > ARR[J] Step 3: SWAP ARR[K] with ARR[POS]
SET SMALL = ARR[J] [END OF LOOP]
SET POS = J Step 4: EXIT
[END OF IF]
[END OF LOOP]
Step 4: RETURN POS

Complexity Analysis of Selection Sort


Time Complexity: O(n2) ,as there are two nested loops:
One loop to select an element of Array one by one = O(n)
Another loop to compare that element with every other Array element = O(n)
Therefore, overall complexity = O(n) * O(n) = O(n*n) = O(n2)
Auxiliary Space: O(1) as the only extra memory used is for temporary variables.
Advantages of Selection Sort
1. Easy to understand and implement, making it ideal for teaching basic
sorting concepts.
2. Requires only a constant O(1) extra memory space.
3. It requires a smaller number of swaps (or memory writes) compared
to many other standard algorithms.
Disadvantages of the Selection Sort
1. Selection sort has a time complexity of O(n^2) makes it slower.
2. Does not maintain the relative order of equal elements which
means it is not stable.

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