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

22CS302_LM7

The document discusses various sorting algorithms used in data structures, including ordered arrays, bubble sort, insertion sort, selection sort, quick sort, and merge sort. It explains key concepts such as internal and external sorting, stability of sorts, and the average time complexities of each algorithm. Each sorting method is described with its properties, best use cases, and steps for implementation.

Uploaded by

poojask1636
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

22CS302_LM7

The document discusses various sorting algorithms used in data structures, including ordered arrays, bubble sort, insertion sort, selection sort, quick sort, and merge sort. It explains key concepts such as internal and external sorting, stability of sorts, and the average time complexities of each algorithm. Each sorting method is described with its properties, best use cases, and steps for implementation.

Uploaded by

poojask1636
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

22XX302 - DATA STRUCTURES I

UNIT I & Ordered Arrays

Ordered Array
 The elements of an ordered array are arranged in ascending (or descending)
order. In general, an ordered array can have duplicate elements. The
elements of an array can be arranged in order by using sort approaches of
the Arrays.
 Sorting is an important process in computer programming. Many data
processing operations depend on data that is sorted, much as the data in a
filing cabinet is kept sorted.
 Sorting an array means to arrange the elements in the array in a certain
order. Various algorithms have been designed that sort the array using
different methods. Some of these sorts are more useful than the others in
certain situations.
Terminologies
Internal/External Sorting

Internal sorting means that all the data that is to be sorted is stored in memory
while sorting is in progress.
External sorting means that the data is stored outside memory (like on disk) and
only loaded into memory in small chunks. External sorting is usually applied in
cases when data can’t fit into memory entirely, effectively allowing to sort data
that does not fit in the memory.
Stability of Sort
A sorting algorithm is said to be stable if two objects with equal keys appear in
the same order in the sorted output as they appear in the unsorted input.
A sorting algorithm is said to be unstable if there are two or more objects with
equal keys which don’t appear in same order before and after sorting.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping
the adjacent elements if they are in wrong order. The pass through the list is
repeated until the list is sorted.
This is an inefficient sort as it has to loop through all the elements multiple times.
It takes O(n^2) time to completely sort the array.
Properties
 Average Time Complexity: O(n^2)
 Stability: Stable
Best use case
This is a very elementary sort which is easy to understand and implement. It is
not recommended in actual production environments. No external memory is
required to sort as it is an in-place sort.

Insertion Sort
In insertion sort, every iteration moves an element from unsorted portion to sorted
portion until all the elements are sorted in the list. An analogy of insertion sort is
the sorting of a deck of cards with our hands. We select one card from the unsorted
deck and put it in the right order in our hands, effectively sorting the whole deck.
Steps
1. Assume that first element in the list is in its sorted portion of the list and
remaining all elements are in unsorted portion.
2. Take the first element from the unsorted list and insert that element into the
sorted list in order specified (ascending or descending).
3. Repeat the above process until all the elements from the unsorted list are
moved into the sorted list.
Properties
 Average Time Complexity: O(n^2)
 Stability: Stable
Best use case
Although this is a elementary sort with the worst case of O(n^2), it performs much
better when the array is nearly sorted, as lesser elements would have to be moved.
It is also preferred when the number of elements are less as it has significantly
less overhead than the other sorts. It consumes less memory and is simpler to
implement.
In some quick sort implementations, insertion sort is internally to sort the smaller
lists faster.
Selection Sort
Selection sort is generally used for sorting files with very large records and small
keys. It selects the smallest (or largest) element in the array and then removes it
to place in a new list. Doing this multiple times would yield the sorted array.
Steps
1. Select the first element of the list.
2. Compare the selected element with all other elements in the list.
3. For every comparison, if any element is smaller (or larger) than selected
element, swap these two elements.
4. Repeat the same procedure with next position in the list till the entire list is
sorted.
Properties
 Average Time Complexity: O(n^2)
 Stability: Non Stable
Best use case
This sort is not influenced by the initial ordering of elements in the array and can
be sued to efficiently sort small lists. It performs the least amount of data
movement amongst all sorts, therefore it could be used where data manipulation
is costly.
Quick Sort
Quick Sort is an efficient divide-and-conquer algorithm. It divides a large list into
two smaller sub-lists based on a pivot chosen, into smaller and larger elements.
Quick Sort then recursively does this to the sub-lists finally producing a sorted
list.
Steps
1. Pick an element, called the pivot.
2. Partitioning: reorder the array so that all elements with values less than the
pivot come before the pivot, while all elements with values greater than the
pivot come after it. After this partitioning, the pivot is in its final position.
This is called the partition operation.
3. Recursively apply the above steps to the sub-array of elements with smaller
values and separately to the sub-array of elements with greater values.
Merge Sort
Merge sort is a very efficient comparison-based sorting algorithm. It is a divide-
and-conquer algorithm, which works by repeatedly dividing the array in small
parts and merging them again in the sorted order.

Steps

1. Divide the unsorted list into n sub-lists, each containing 1 element.


2. Repeatedly merge the sub-lists to produce new sorted sub-lists until only 1
sub-list remains. This will be the final sorted list.Code for merge sort

You might also like