0% found this document useful (0 votes)
10 views42 pages

1729172959-UNIT-V Internal Sorting

The document covers various internal sorting algorithms, including Insertion Sort, Quick Sort, Merge Sort, and Heap Sort, as part of a Data Structures course. Each sorting method is explained with procedures and examples, highlighting their characteristics and applications. Additionally, problem-solving techniques such as Brute Force, Greedy Algorithm, Divide and Conquer, Dynamic Programming, and Backtracking are mentioned.

Uploaded by

supravat.p2005
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)
10 views42 pages

1729172959-UNIT-V Internal Sorting

The document covers various internal sorting algorithms, including Insertion Sort, Quick Sort, Merge Sort, and Heap Sort, as part of a Data Structures course. Each sorting method is explained with procedures and examples, highlighting their characteristics and applications. Additionally, problem-solving techniques such as Brute Force, Greedy Algorithm, Divide and Conquer, Dynamic Programming, and Backtracking are mentioned.

Uploaded by

supravat.p2005
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/ 42

Department of Computer Science

Subject Name : Data Structures

Subject Code : 33A

Class : II B.Sc. CS

Semester : III

Prepared by : Mr. A.P Christopher Arokiaraj


Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit – V
INTERNAL SORTING: Searching,
Insertion Sort
Searching
• A file is a collection of records.
• The fields used to distinguish among the records are known as keys.
• Since the same file may be used for several different applications, the
key fields for record identification will depend on the particular
application.
• For instance, we may regard a telephone directory as a file, each
record having three fields: name, address, and phone number.
• The key is usually the person's name.
Sequential Search

• A sequential file F and we wish to retrieve a record with a certain


key value K.
• If F has n records with Ki the key value for record Ri, then one
may carry out the retrieval by examining the key values Kn,Kn -
1, ...,K1 in that order, until the correct record is located.
• Such a search is known as sequential search since the records are
examined sequentially.
Sequential Search

procedure SEQSRCH(F,n,i,K)
//Search a file F with key values K1, ...,Kn for a record Ri such that
Ki = K. If there is no such record, i is set to 0//
K0← K; i ← n;
while Ki ≠ K do
i←i-1
end
end SEQSRCH
Methods of Sorting
• To begin with we characterize sorting methods into two broad
categories:
(i) internal methods, i.e., methods to be used when the file to be
sorted is small enough so that the entire sort can be carried out in
main memory; and
(ii) external methods, i.e., methods to be used on larger files. The
following internal sorting
methods:
a) Insertion sort
b) Quick sort
c) Merge sort
d) Heap sort
e) Radix sort
INSERTION SORT

• The basic step in this method is to insert a record R into a sequence of


ordered records, R1,R2, ...,Ri, (K1 K2, ..., Ki) in such a way that the
resulting sequence of size i + 1 is also ordered.
• The algorithm below accomplishes this insertion.
• It assumes the existence of an artificial record Ro with key Ko = - (i.e., all
keys are Ko).
PROCEDURE INSERT
procedure INSERT (R,i)
//Insert record R with key K into the ordered sequence Ro, ...,Ri in such a way
that the resulting sequence is also ordered on key K. We assume that Ro is a
record (maybe a dummy) such that K Ko//
j i
while K < Kj do
//move Rj one space up as R is to be inserted left of Rj//
Rj+1 Rj; j j-1
end
Rj+1 R
end INSERT
Analysis of INSERTION SORT

procedure INSORT(R,n)
//sort the records R1, ...,Rn in nondecreasing value of the key K. Assume n > 1//
Ko - //Create a dummy record Ro such that Ko < Ki, 1 i n//
for j 2 to n do
T Rj
call INSERT(T, j - 1) //insert records R2 to Rn//
end
end INSORT
Example: Assume n = 5 and the input sequence
is (5,4,3,2,1). Then, after each insertion we have
the following.

•- , 5, 4, 3, 2, 1 [initial sequence]
•- , 4, 5, 3, 2, 1 i=2
•- , 3, 4, 5, 2, 1 i=3
•- , 2, 3, 4, 5, 1 i=4
•- , 1, 2, 3, 4, 5 i=5
THANK YOU
Department of Computer Science

Subject Name : Data Structures

Subject Code : 33A

Class : II B.Sc. CS

Semester : III

Prepared by : Mr. Christopher Arokiaraj


Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - V
QUICK SORT
INTRODUCTION
In Insertion Sort the key Ki currently controlling the insertion is placed into
the right spot with respect to the sorted subfile (R1, ...,Ri - 1).
Quicksort differs from insertion sort in that the key Ki controlling the process
is placed at the right spot with respect to the whole file.
Thus, if key Ki is placed in position s(i), then Kj Ks(i) for j < s(i) and Kj Ks(i)
for j > s(i).
Problem Solving techniques/approaches
Most commonly used problem solving approaches for solving Data Structures and Algorithmic
problems.

1. Brute force approach


2. Greedy Algorithm
3.Divide & Conquer
4. Dynamic Programming
5. Backtracking
Quick Sort
Like Merge Sort, QuickSort uses Divide and Conquer problem solving technique.
It picks an element as pivot and partitions the given array around the picked pivot.
There are many different versions of QuickSort that pick pivot in different ways.
i) Always pick first element as pivot.
ii) Always pick last element as pivot (implemented below)
iii) Pick a random element as pivot.
iv) Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element
x of array as pivot, put x at its correct position in sorted array and put all smaller elements
(smaller than x) before x, and put all greater elements (greater than x) after x. All this should be
done in linear time.
…contd.
The quick sort uses divide and conquer to gain the same advantages as the merge
sort, while not using additional storage.
As a trade-off, however, it is possible that the list may not be divided in half.
When this happens, we will see that performance is diminished.
A quick sort first selects a value, which is called the pivot value.
Although there are many different ways to choose the pivot value, we will
simply use the first item in the list.
The role of the pivot value is to assist with splitting the list.
The actual position where the pivot value belongs in the final sorted list,
commonly called the split point, will be used to divide the list for subsequent
calls to the quick sort.
…contd.
Quicksort is an efficient internal sorting algorithm with best computing time.
Developed by British computer scientist Tony Hoare in 1959 and published in 1961, it is still
a commonly used algorithm for sorting.
When implemented well, it can be about two or three times faster than its main competitors,
merge sort and heapsort.
Example
PROCEDURE QSORT
procedureQSORT(m,n)
//sort records Rm, ...,Rn into nondecreasing order on key K. Key Km is arbitrarily
chosen as the control key. Pointers i and j are used to partition the subfile so that at
any time Kl≤K, l<I andKl≥ K, l>j. It is assumed that Km≤Kn+1//
if m < n
then [i🡨 m; j🡨 n + 1; K🡨 Km
loop
repeat i🡨 i + 1 until Ki≥K; repeat j🡨 j - 1
until Kj≤K; if i< j
then call INTERCHANGE (R(i),R(j))
else exit forever
call INTERCHANGE (R(m),R(j))
call QSORT (m,j - 1)
call QSORT (j + 1, n)]
end QSORT
EXAMPLE: The input file has 10 records
with keys.
Department of Computer Science

Subject Name : Data Structures

Subject Code : 33A

Class : II B.Sc. CS

Semester : III

Prepared by : Mr. A.P Christopher Arokiaraj


Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - V
2 WAY MERGE SORT
2 WAY MERGE SORT
• The merge sort algorithm to sort n records let us see how one may merge two files (Xl, ...,Xm) and (Xm+1,
...,Xn) that are already sorted to get a third file (Zl, ...,Zn) that is also sorted. S
• This merging scheme is very simple.
procedureMERGE(X,l,m,n,Z)

//(Xl, ...,Xm) and (Xm+1, ...,Xn) are two sorted files with keys xl≤... ≤xm and Xm+1 ≤ ... ≤xn.
They are merged to obtain the sorted file (Zl, ..., Zn) such that zl≤ ... ≤zn// i🡨 k🡨 l; j🡨 m +
1 //i, j and k are position in the three files//
While i≤ m and j≤n do
if xi ≤xj then [Zk🡨 Xi; i🡨 i+ 1]
else [Zk🡨 Xj; j 🡨 j+ 1] k🡨 k + 1
end
if i>m then (Zk, ...,Zn) 🡨 (Xj, ...,Xn)
else (Zk, ...,Zn) 🡨 (Xi ...,Xm)
end MERGE
Example1 :

• The input file is (26, 5, 77, 1, 61, 11, 59, 15, 15, 48, 19). The tree below illustrates the subfiles
being merged at each pass:
Example
Example
Department of Computer Science

Subject Name : Data Structures

Subject Code : 33A

Class : II B.Sc. CS

Semester : III

Prepared by : Mr.A.P Christopher Arokiaraj


Assistant Professor
Department of Computer Science
KG College of Arts and Science
Unit - V
HEAP SORT
HEAP SORT
• Heap is a special case of balanced binary tree data structure where
the root-node key is compared with its children and arranged
accordingly.
• This method will interpret the file to be sorted R = (R1, ...,Rn) as a binary tree.
• The parent of the node at location i is at i/2, the left child at 2i and the right child at 2i + 1. If 2i or
2i + 1 is greater than n
Methods
• 1st stage method
• First the tree representing the file is converted into a heap.
• A heap is defined to be a complete binary tree with the property that the
value of each node is at least as large as the value of its children nodes(i.e., K
j/2 Kj for 1 j/2 < j n).
• This implies that the root of the heap has the largest key in the tree.
Second stage method

• In the second stage the output sequence is generated in decreasing order by


successively outputting the root and restructuring the remaining tree into a
heap.
• procedure ADJUST (i,n)
• //Adjust the binary tree with root i to satisfy the heap property. The left and right subtrees
of i, i.e., with roots 2i and 2i+ 1, already satisfy the heap property. The nodes of the trees
contain records, R, with keys K. No node has index greater than n//

• R←Ri; K ← Ki; j ← 2i
• while j ≤n do
• if j<n and Kj <Kj+1 then j ← j + 1 //find max of left and right child//
• //compare max. child with K. If K is max. then done//
• if K ≥ Kj then[R j/2 ← R; return]
• R j/2 ← Rj; j ← 2j //move Rj up the tree//
• end
• R j/2 ← R
• end ADJUST
Example1

• The input file is (26, 5, 77, 1, 61, 11, 59, 15, 48, 19). Interpreting this as a
binary tree we have the following transformations:

You might also like