01 - Sorting Problem
01 - Sorting Problem
AND
A LGORITHMS
E DITED B Y
A RSHIA G HAROONI
The University of central Tehran branch
Tehran
2023
1
0.1 Introduction
Sorting is the process of arranging a collection of items in a specific order,
usually ascending or descending. Sorting is one of the most fundamental
operations in computer science and is used in a wide range of applications,
including data processing, searching, and machine learning.
Sorting algorithms are the algorithms that sort a given set of data into a
specific order. There are various sorting algorithms available, each with its
own advantages and disadvantages. In this lecture note, we will discuss the
two popular sorting algorithms: Insertion Sort and Merge Sort.
for i = 1 to n-1
j = i
while j > 0 and array[j-1] > array[j]
swap array[j] and array[j-1]
j = j - 1
1. Starting from the second element of the array, iterate over the unsorted
part of the array.
2. Take the current element and compare it with the elements in the
sorted part of the array.
2
3. Move the larger elements one position to the right to make room for the
current element.
4. Insert the current element in the proper place in the sorted part of the
array.
5. Repeat steps 2 to 4 for all elements in the unsorted part of the array.
2. Low overhead: Insertion Sort has a low overhead, as it does not use any
extra space apart from the input array.
3. Good for small arrays or mostly sorted arrays: Insertion Sort performs
well on small arrays or arrays that are mostly sorted, as it does not
need to perform many comparisons and swaps.
2. Not suitable for complex data structures: Insertion Sort is not suitable
for complex data structures, such as trees or graphs, as it requires a
linear data structure
3
3. Comb Sort: Comb Sort is a variation of Insertion Sort that uses a larger
gap between elements to compare, reducing the number of swaps re-
quired.
discuss Merge Sort in detail, including its algorithm, time complexity, space
complexity, advantages, and disadvantages.
function merge_sort(array)
if length(array) <= 1
return array
else
middle = length(array) / 2
left_half = merge_sort(array[0:middle])
right_half = merge_sort(array[middle:length(array)])
4
1. Check if the length of the array is less than or equal to 1. If it is, return
the array as it is already sorted.
2. Divide the array into two halves, using the middle index.
The Merge function takes two sorted arrays as input and produces a sin-
gle sorted array as output. The function works by iterating over the elements
of the two arrays and inserting them in the proper order in a new array.
3. Suitable for linked lists: Merge Sort is suitable for linked lists as it does
not require random access to elements in the array.
3. Not in-place: Merge Sort requires additional memory to store the result
of the merge operation. This can be a disadvantage in situations where
memory is limited.
3. Hybrid Merge Sort: Hybrid Merge Sort is a variation of Merge Sort that
uses a combination of Merge Sort and another sorting algorithm, such
as Insertion Sort or Quick Sort, to improve efficiency.
6
2. Quick Sort: Quick Sort is another sorting algorithm that uses the Di-
vide and Conquer technique. It works by selecting a pivot element from
the array, dividing the array into two subarrays based on the pivot el-
ement, recursively sorting each subarray, and then combining the re-
sults.