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

Sorting

The document provides an overview of sorting algorithms, specifically focusing on insertion sort and the divide-and-conquer approach used in merge sort. It details the pseudocode for insertion sort, its analysis, and the concept of running time in relation to input size. Additionally, it introduces the merge sort algorithm, which operates by dividing the input into smaller subsequences, sorting them, and then combining the results.

Uploaded by

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

Sorting

The document provides an overview of sorting algorithms, specifically focusing on insertion sort and the divide-and-conquer approach used in merge sort. It details the pseudocode for insertion sort, its analysis, and the concept of running time in relation to input size. Additionally, it introduces the merge sort algorithm, which operates by dividing the input into smaller subsequences, sorting them, and then combining the results.

Uploaded by

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

Data Structure

ICT2101
Dr. Mohammad Abu Yousuf
[email protected]

1
Some Sorting Algorithm

2
Insertion sort (1)

3
Insertion sort (2)

• Pseudocode for insertion sort as a procedure called


INSERTIONSORT, which takes as a parameter an array
A[1…n] containing a sequence of length n that is to be
sorted. (In the code, the number n of elements in A is
denoted by A.length.)
• The algorithm sorts the input numbers in place: it
rearranges the numbers within the array A, with at most
a constant number of them stored outside the array at
any time.
• The input array A contains the sorted output sequence
when the INSERTION-SORT procedure is finished.
4
Insertion sort (3)

5
Insertion sort (4)

6
Insertion sort (5)

7
Analyzing algorithms(1)
Analysis of insertion sort:
• The time taken by the INSERTION-SORT procedure
depends on the input: sorting a thousand numbers takes
longer than sorting three numbers.

• Moreover, INSERTIONSORT can take different amounts of


time to sort two input sequences of the same size
depending on how nearly sorted they already are.

• In general, the time taken by an algorithm grows with


the size of the input, so it is traditional to describe the
running time of a program as a function of the size of its
input.

• To do so, we need to define the terms “running time” and


“size of input” more carefully.
8
Analyzing algorithms(2)

• The best notion for input size depends on the problem


being studied. For many problems, such as sorting or
computing discrete Fourier transforms, the most natural
measure is the number of items in the input—for example,
the array size n for sorting.

• For many other problems, such as multiplying two integers,


the best measure of input size is the total number of bits
needed to represent the input in ordinary binary notation.

• Sometimes, it is more appropriate to describe the size of the


input with two numbers rather than one. For instance, if the
input to an algorithm is a graph, the input size can be
described by the numbers of vertices and edges in the
graph.

9
Analyzing algorithms(3)

• The running time of an algorithm on a particular input is


the number of primitive operations or “steps” executed.

• It is convenient to define the notion of step so that it is as


machine-independent as possible. For the moment, let
us adopt the following view. A constant amount of time is
required to execute each line of our pseudocode.

• One line may take a different amount of time than


another line, but we shall assume that each execution of
the ith line takes time ci, where ci is a constant.

10
Analyzing algorithms(4)

• We start by presenting the INSERTION-SORT procedure


with the time “cost” of each statement and the number
of times each statement is executed.

• For each j = 2,3,……,n, where n = A.length, we let tj


denote the number of times the while loop test in line 5
is executed for that value of j. When a for or while loop
exits in the usual way (i.e., due to the test in the loop
header), the test is executed one time more than the
loop body.
• We assume that comments are not executable
statements, and so they take no time. 11
Analyzing algorithms(5)

12
Analyzing algorithms(6)

13
Analyzing algorithms(7)

14
15
Order of growth
• We used some simplifying abstractions to ease our analysis of the
INSERTIONSORT procedure.

• First, we ignored the actual cost of each statement, using the constants ci
to represent these costs.

• Then, we observed that even these constants give us more detail than we
really need: we expressed the worst-case running time as an2 + bn + c for
some constants a, b, and c that depend on the statement costs ci . We
thus ignored not only the actual statement costs, but also the abstract
costs ci .

• We shall now make one more simplifying abstraction: it is the rate of


growth, or order of growth, of the running time that really interests us.

• We therefore consider only the leading term of a formula (e.g., an2),


since the lower-order terms are relatively insignificant for large
values of n.
16
Divide and Conquer algorithms

17
The divide-and-conquer approach

• For insertion sort, we used an incremental approach:


having sorted the sub-array A[1…. j -1], we inserted
the single element A[j] into its proper place, yielding
the sorted sub-array A[1……j] .

• we examine an alternative design approach, known


as “divide and conquer,”. We’ll use divide and
conquer to design a sorting algorithm whose
worst-case running time is much less than that of
insertion sort.

18
The divide-and-conquer approach

• The divide-and-conquer paradigm involves three steps


at each level of the recursion:
Divide the problem into a number of sub problems
that are smaller instances of the same problem.
Conquer the sub problems by solving them recursively.
If the sub problem sizes are small enough, however,
just solve the sub problems in a straightforward
manner.
Combine the solutions to the sub problems into the
solution for the original problem.

19
Merge sort algorithm

• The merge sort algorithm closely follows the


divide-and-conquer paradigm. Intuitively, it operates as
follows.
Divide: Divide the n-element sequence to be sorted
into two subsequences of n=2 elements each.
Conquer: Sort the two subsequences recursively using
merge sort.
Combine: Merge the two sorted subsequences to
produce the sorted answer

20
Merge sort algorithm

21
22

Merge sort algorithm


Merge sort algorithm

23
24
25
Merge sort algorithm

26
Merge sort algorithm

27
Thank you

28

You might also like