SlideShare a Scribd company logo
SANJIVANI K. B. P. POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Department:- Computer Technology Class:- CM3I
Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317
Name of Faculty: Prof. Vaibhav A. Parjane
Searching and Sorting
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Unit Outcome
After going through this unit, the student will be able to:
2a. Explain working of the given search method with an example.
2b. Write an algorithm to search the given key using binary Search method
2c. Write an Algorithm to sort data using a specified sorting method.
2d. Explain the working of given sorting method step by-step with an
example and small data set.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Introduction: (Searching)
• Searching: Searching is a process of finding the location of a particular
element in an array is called searching.
• There are two types of searching:
 Linear Search:
 Binary Search:
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Linear Search
• Linear search or sequential search is a method for finding a particular
value in a list that consists of checking every one of its elements
• One element at a time and in sequence, until the desired one is
found.
• It is simplest search algorithm
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Linear Search
• Consider an array of 20 elements.
• Suppose, element 8 is to be search in the array.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Linear Search (Algorithm)
Let us start with an array or list, L which may have the item in question.
Step 1: If the list L is empty, then the list has nothing. The list does not
have the item in question. Stop here.
Step 2: Otherwise, look at all the elements in the list L.
Step 3: For each element: If the element equals the item in question,
the list contains the item in question. Stop here. Otherwise, go onto
next element.
Step 4: The list does not have the item in question.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Linear Search (Program )
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
# 𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 >
𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂𝒓𝒓[𝟓𝟎], 𝒏, 𝒊, 𝒊𝒕𝒆𝒎;
𝒑𝒓𝒊𝒏𝒕𝒇("𝑯𝒐𝒘 𝒎𝒂𝒏𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒚𝒐𝒖 𝒘𝒂𝒏𝒕 𝒕𝒐 𝒆𝒏𝒕𝒆𝒓 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚
∶ ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏);
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊 + 𝟏);
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]);
}
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 ∶ ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒊𝒕𝒆𝒎);
Linear Search (Program )
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
𝒇𝒐𝒓( 𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
// 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 𝒊𝒕𝒆𝒎 𝒊𝒔 𝒄𝒐𝒎𝒑𝒂𝒓𝒆𝒅 𝒘𝒊𝒕𝒉 𝒂𝒓𝒓𝒂𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕
𝒊𝒇 𝒊𝒕𝒆𝒎 == 𝒂𝒓𝒓 𝒊
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒇𝒐𝒖𝒏𝒅 𝒂𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 %𝒅𝒏", 𝒊𝒕𝒆𝒎, 𝒊
+ 𝟏);
𝒃𝒓𝒆𝒂𝒌;
}
}
𝒊𝒇(𝒊 == 𝒏)
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑰𝒕𝒆𝒎 %𝒅 𝒏𝒐𝒕 𝒇𝒐𝒖𝒏𝒅 𝒊𝒏 𝒂𝒓𝒓𝒂𝒚𝒏", 𝒊𝒕𝒆𝒎);
𝒈𝒆𝒕𝒄𝒉();
}
Continued……
Binary Search
• Also known as half-interval search algorithm.
• Finds the position of a specified input value within an array sorted by
key value.
• For binary search, the array should be arranged in ascending or
descending order.
• The binary search is based on the divide-and-conquer approach.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Binary Search
• In this technique, the element to be searched (say, item) is compared
with the middle element of the array.
• If item is equal to the middle element, then search is successful.
• If the item is smaller than the middle element, then item is searched
in the segment of the array before the middle element. If the item is
greater than the middle element, item is searched in the array
segment after the middle element.
• This process will be in iteration until the element is found or the array
segment is reduced to a single element that is not equal to item.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Binary Search
• Consider an sorted array of 11 elements as shown in figure below.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Suppose we want to search the element 55 from the array of elements.
Binary Search
• For this we will take 3 variables Start, End and Middle
• Which will keep track of the status of start, end and middle value of the portion
of the array, in which we will search the element.
• The value of the middle will be as:
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
𝑴𝒊𝒅𝒅𝒍𝒆 =
𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅
2
• Initially, 𝑺𝒕𝒂𝒓𝒕 = 𝟏 𝑬𝒏𝒅 = 𝟏𝟏 and 𝑴𝒊𝒅𝒅𝒍𝒆 = (𝟏 + 𝟏𝟏) / 𝟐 = 𝟔.
• The key element 55 is to be compared with the Middle value.
• The value at index 6 is 48 and it is smaller than the target value i.e. (55).
Binary Search
• If the key is less than the value at Middle then the key element is
present in the first half else in other half;
• In our case, the key is on the right half.
• Hence, now Start = Middle + 1 = 6 + 1 =7.
• Calculate the middle index of the second half
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 11 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 9
Binary Search
• Again, the Middle divides the second half into the two parts as shown
in fig below
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
• The key element 55 is lesser than the value at Middle which is 72
• Hence it is present in the left half i.e., towards the left of 72.
• Hence now, End = Middle – 1 = 8. Start will remain 7
Binary Search
• At last, the Middle is calculated as
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
• Now if key element 55 is compared with the value at middle, then it is found
that they are equal.
• The key element is searched successfully and it is in 7th location
𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 8 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7
Binary Search (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
1.Start with the middle element:
•If the target value is equal to the middle element of the array, then return the index of the
middle element.
•If not, then compare the middle element with the target value,
•If the target value is greater than the number in the middle index, then pick the elements to the
right of the middle index, and start with Step 1.
•If the target value is less than the number in the middle index, then pick the elements to the left of
the middle index, and start with Step 1.
2.When a match is found, return the index of the element matched.
3.If no match is found, then return -1
Binary Search (Program )
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 >
𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂𝒓𝒓[𝟐𝟎], 𝒔𝒕𝒂𝒓𝒕, 𝒆𝒏𝒅, 𝒎𝒊𝒅𝒅𝒍𝒆, 𝒏, 𝒊, 𝒊𝒕𝒆𝒎;
𝒄𝒍𝒓𝒔𝒄𝒓();
𝒑𝒓𝒊𝒏𝒕𝒇("𝑯𝒐𝒘 𝒎𝒂𝒏𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒚𝒐𝒖 𝒘𝒂𝒏𝒕 𝒕𝒐 𝒆𝒏𝒕𝒆𝒓 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚 ∶ ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏);
𝒇𝒐𝒓(𝒊 = 𝟏; 𝒊 <= 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊);
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]);
}
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 ∶ ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒊𝒕𝒆𝒎);
// 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 𝒊𝒔 𝒔𝒕𝒐𝒓𝒆𝒅 𝒊𝒏 𝒗𝒂𝒓𝒊𝒂𝒃𝒍𝒆 𝒊𝒕𝒆𝒎
Binary Search (Program )
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒔𝒕𝒂𝒓𝒕 = 𝟏;
𝒆𝒏𝒅 = 𝒏;
𝒎𝒊𝒅𝒅𝒍𝒆 = (𝒔𝒕𝒂𝒓𝒕 + 𝒆𝒏𝒅) / 𝟐;
𝒘𝒉𝒊𝒍𝒆(𝒊𝒕𝒆𝒎! = 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆] && 𝒔𝒕𝒂𝒓𝒕 <= 𝒆𝒏𝒅)
{
𝒊𝒇(𝒊𝒕𝒆𝒎 > 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆])
𝒔𝒕𝒂𝒓𝒕 = 𝒎𝒊𝒅𝒅𝒍𝒆 + 𝟏;
𝒆𝒍𝒔𝒆
𝒆𝒏𝒅 = 𝒎𝒊𝒅𝒅𝒍𝒆 − 𝟏;
𝒎𝒊𝒅𝒅𝒍𝒆 = (𝒔𝒕𝒂𝒓𝒕 + 𝒆𝒏𝒅) / 𝟐;
}
𝒊𝒇(𝒊𝒕𝒆𝒎 == 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆])
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒊𝒔 𝒇𝒐𝒖𝒏𝒅 𝒂𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 %𝒅𝒏", 𝒊𝒕𝒆𝒎, 𝒎𝒊𝒅𝒅𝒍𝒆);
𝒊𝒇(𝒔𝒕𝒂𝒓𝒕 > 𝒆𝒏𝒅)
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒊𝒔 𝒏𝒐𝒕 𝒇𝒐𝒖𝒏𝒅 𝒊𝒏 𝒂𝒓𝒓𝒂𝒚𝒏", 𝒊𝒕𝒆𝒎);
𝒈𝒆𝒕𝒄𝒉();
}
Introduction (Sorting)
• Sorting: Sorting refers to arranging data in a particular format.
• Sorting means arranging the data in ascending or descending order.
• There are the several sorting techniques:
 Bubble Sort
 Selection Sort
 Insertion Sort
 Quick Sort
 Radix Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Bubble Sort (Algorithm)
Following are the steps involved in bubble sort(for sorting a given array
in ascending order):
1. Starting with the first element(index = 0), compare the current
element with the next element of the array.
2. If the current element is greater than the next element of the array,
swap them.
3. If the current element is less than the next element, move to the
next element. Repeat Step1.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Bubble Sort
Let's consider an array with values {5, 1, 6, 2, 4, 3}
Next, we have a pictorial representation of how bubble sort will sort
the given array.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
5 1 6 2 4 3
5
1 6 2 4 3
1 5 6 2 4 3
1 5 2 6 4 3
1 5 4
2 6 3
1 5 2 4 3 6
Iteration 1
5 > 1 Interchange
5 < 6 No change
6 > 2 Interchange
6 > 4 Interchange
6 > 3 Interchange
Bubble Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
2
1 5 4 3 6
1 2 4 5 3 6
Iteration 2
5 > 2 Interchange
5 > 4 Interchange
5 > 3 Interchange
1 5 2 4 3 6
1 2 4 3 5 6
1 < 5 No change 1 5 2 4 3 6
Bubble Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Iteration 3
2 < 4 No change
4 > 3 Interchange
1 2 4 3 5 6
1 < 2 No change
1 2 4 3 5 6
1 2 4 3 5 6
1 2 3 4 5 6
After Iteration 5 we get all elements sorted
1 2 3 4 5 6
Bubble Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Bubble Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒓𝒊𝒏𝒈. 𝒉 >
𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂[𝟏𝟎], 𝒊, , 𝒋, 𝒌, 𝒕𝒆𝒎𝒑, 𝒏, 𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔;
𝒄𝒍𝒓𝒔𝒄𝒓();
𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 ∶ 𝒎𝒂𝒙 𝟐𝟓");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏);
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊 + 𝟏);
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂[𝒊]);
}
𝒑𝒓𝒊𝒏𝒕𝒇("𝑼𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏");
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
Bubble Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
/∗ 𝑩𝒖𝒃𝒃𝒍𝒆 𝒔𝒐𝒓𝒕 ∗/
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏 ; 𝒊 + +)
{
𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 = 𝟎;
𝒇𝒐𝒓 (𝒋 = 𝟎; 𝒋 < 𝒏 − 𝟏 − 𝒊; 𝒋 + +)
{
𝒊𝒇 (𝒂[𝒋] > 𝒂[𝒋 + 𝟏])
{
𝒕𝒆𝒎𝒑 = 𝒂[𝒋];
𝒂[𝒋] = 𝒂[𝒋 + 𝟏];
𝒂[𝒋 + 𝟏] = 𝒕𝒆𝒎𝒑;
𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 + +;
} /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒊𝒇 ∗/
} /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒇𝒐𝒓 𝒍𝒐𝒐𝒑 ∗/
𝒊𝒇(𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 == 𝟎) /∗ 𝑰𝒇 𝒍𝒊𝒔𝒕 𝒊𝒔 𝒔𝒐𝒓𝒕𝒆𝒅 ∗/
𝒃𝒓𝒆𝒂𝒌;
𝒑𝒓𝒊𝒏𝒕𝒇("𝑨𝒇𝒕𝒆𝒓 𝑷𝒂𝒔𝒔 %𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒂𝒓𝒆 ∶ ", 𝒊 + 𝟏);
𝒇𝒐𝒓 (𝒌 = 𝟎; 𝒌 < 𝒏; 𝒌 + +)
𝒑𝒓𝒊𝒏𝒕𝒇 %d , 𝒂 𝒌 ;
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
} /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒐𝒖𝒕𝒆𝒓 𝒇𝒐𝒓 𝒍𝒐𝒐𝒑 ∗/
Bubble Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏");
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]);
𝒈𝒆𝒕𝒄𝒉();
}
Bubble Sort (Program using function)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
𝒗𝒐𝒊𝒅 𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏)
{
𝒊𝒏𝒕 𝒊, 𝒋, 𝒕𝒆𝒎𝒑;
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒇𝒐𝒓(𝒋 = 𝟎; 𝒋 < 𝒏 − 𝒊 − 𝟏; 𝒋 + +)
{
// 𝒇𝒍𝒂𝒈 𝒕𝒐 𝒎𝒐𝒏𝒊𝒕𝒐𝒓 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈
𝒊𝒏𝒕 𝒇𝒍𝒂𝒈 = 𝟎;
𝒊𝒇( 𝒂𝒓𝒓[𝒋] > 𝒂𝒓𝒓[𝒋 + 𝟏])
{
// 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈
𝒕𝒆𝒎𝒑 = 𝒂𝒓𝒓[𝒋];
𝒂𝒓𝒓[𝒋] = 𝒂𝒓𝒓[𝒋 + 𝟏];
𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒕𝒆𝒎𝒑;
// 𝒊𝒇 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈 𝒉𝒂𝒑𝒑𝒆𝒏𝒔 𝒖𝒑𝒅𝒂𝒕𝒆 𝒇𝒍𝒂𝒈 𝒕𝒐 𝟏
𝒇𝒍𝒂𝒈 = 𝟏;
}
}
Bubble Sort (Program using function)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
// 𝒊𝒇 𝒗𝒂𝒍𝒖𝒆 𝒐𝒇 𝒇𝒍𝒂𝒈 𝒊𝒔 𝒛𝒆𝒓𝒐 𝒂𝒇𝒕𝒆𝒓 𝒂𝒍𝒍 𝒕𝒉𝒆 𝒊𝒕𝒆𝒓𝒂𝒕𝒊𝒐𝒏𝒔 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒍𝒐𝒐𝒑
// 𝒕𝒉𝒆𝒏 𝒃𝒓𝒆𝒂𝒌 𝒐𝒖𝒕
𝒊𝒇(! 𝒇𝒍𝒂𝒈)
{
𝒃𝒓𝒆𝒂𝒌;
}
}
// 𝒑𝒓𝒊𝒏𝒕 𝒕𝒉𝒆 𝒔𝒐𝒓𝒕𝒆𝒅 𝒂𝒓𝒓𝒂𝒚
𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝑨𝒓𝒓𝒂𝒚: ");
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓[𝒊]);
}
}
Bubble Sort (Program using function)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒊𝒏𝒕 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂𝒓𝒓[𝟏𝟎𝟎], 𝒊, 𝒏, 𝒔𝒕𝒆𝒑, 𝒕𝒆𝒎𝒑;
// 𝒂𝒔𝒌 𝒖𝒔𝒆𝒓 𝒇𝒐𝒓 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒕𝒐 𝒃𝒆 𝒔𝒐𝒓𝒕𝒆𝒅
𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒕𝒐 𝒃𝒆 𝒔𝒐𝒓𝒕𝒆𝒅: ");
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏);
// 𝒊𝒏𝒑𝒖𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒏𝒐. %𝒅: ", 𝒊 + 𝟏);
𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]);
}
// 𝒄𝒂𝒍𝒍 𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕
𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝒏);
}
Selection Sort
• Selection sort is conceptually the most simplest sorting algorithm.
• This algorithm will first find the smallest element in the array and
swap it with the element in the first position.
• Then it will find the second smallest element and swap it with the
element in the second position
• It will repeat this until the entire array is sorted.
• It is called selection sort because it repeatedly selects the next-
smallest element and swaps it into the right place.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Selection Sort (Algorithm)
Following are the steps involved in selection sort(for sorting a given
array in ascending order):
Step 1 - Set MIN to location 0
Step 2 - Search the minimum element in the list
Step 3 - Swap with value at location MIN
Step 4 - Increment MIN to point to next element
Step 5 - Repeat until list is sorted
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Selection Sort (Algorithm)
Following are the steps involved in selection sort(for sorting a given array in ascending
order):
1. Set the first element as minimum
2. Compare minimum with the second element.
3. If the second element is smaller than minimum, assign the second element as
minimum.
4. Compare minimum with the third element.
5. Again, if the third element is smaller, then assign minimum to the third element
otherwise do nothing.
6. The process goes on until the last element.
7. After each iteration, minimum is placed in the front of the unsorted list.
8. For each iteration, indexing starts from the first unsorted element.
9. Step 1 to 3 are repeated until all the elements are placed at their correct positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Step 1
21 11 8 13 1
21 11 8 13 1
21 11 8 13 1
21 11 8 13 1
1 11 8 13 21
Swapping
𝒊 = 𝟎
𝒊 = 𝟏
𝒊 = 𝟐
𝒊 = 𝟑
Min value at index 1
Min value at index 2
Min value at index 2
Min value at index 4
First Iteration
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Step 1
𝒊 = 𝟎
𝒊 = 𝟏
𝒊 = 𝟐
Min value at index 2
Min value at index 2
Min value at index 2
Second Iteration
1 11 8 13 21
1 11 8 13 21
1 11 8 13 21
1 8 11 13 21
Swapping
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Step 1
𝒊 = 𝟎
𝒊 = 𝟏
Min value at index 2
Min value at index 2
Already in Place
Third Iteration
1 8 11 13 21
1 8 11 13 21
1 8 11 13 21
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Step 1
𝒊 = 𝟎
𝒊 = 𝟏
Min value at index 3
Fourth Iteration
1 8 11 13 21
1 8 11 13 21
Selection Sort
Already in Place
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Selection Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 >
𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂[𝟐𝟓], 𝒊, , 𝒋, 𝒌, 𝒏, 𝒕𝒆𝒎𝒑, 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕;
𝒄𝒍𝒓𝒔𝒄𝒓();
𝒑𝒓𝒊𝒏𝒕𝒇(“𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 ∶ “);
𝒔𝒄𝒂𝒏𝒇(“%𝒅”, &𝒏);
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
{
𝒑𝒓𝒊𝒏𝒕𝒇(“𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ “, 𝒊 + 𝟏);
𝒔𝒄𝒂𝒏𝒇(“%𝒅”, &𝒂[𝒊]);
}
/∗ 𝑫𝒊𝒔𝒑𝒍𝒂𝒚 𝒕𝒉𝒆 𝒖𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 ∗/
𝒑𝒓𝒊𝒏𝒕𝒇(“𝑼𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏”);
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇(“%𝒅 “, 𝒂[𝒊]);
𝒑𝒓𝒊𝒏𝒕𝒇(“𝒏”);
Selection Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
/∗ 𝑺𝒆𝒍𝒆𝒄𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏 ; 𝒊 + +)
{
/∗ 𝑭𝒊𝒏𝒅 𝒕𝒉𝒆 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 ∗/
𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 = 𝒊;
𝒇𝒐𝒓(𝒌 = 𝒊 + 𝟏; 𝒌 < 𝒏 ; 𝒌 + +)
{
𝒊𝒇(𝒂[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕] > 𝒂[𝒌])
𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 = 𝒌 ;
}
𝒊𝒇( 𝒊 ! = 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 )
{
𝒕𝒆𝒎𝒑 = 𝒂 [𝒊];
𝒂[𝒊] = 𝒂[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕];
𝒂𝒓𝒓[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕] = 𝒕𝒆𝒎𝒑 ;
}
𝒑𝒓𝒊𝒏𝒕𝒇("𝑨𝒇𝒕𝒆𝒓 𝑷𝒂𝒔𝒔 %𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒂𝒓𝒆 ∶ ", 𝒊 + 𝟏);
𝒇𝒐𝒓 (𝒋 = 𝟎; 𝒋 < 𝒏; 𝒋 + +)
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒋]);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
} /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒇𝒐𝒓 ∗/
Selection Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
/∗ 𝑺𝒆𝒍𝒆𝒄𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/
𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏");
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
𝒈𝒆𝒕𝒄𝒉();
} /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒎𝒂𝒊𝒏() ∗/
Insertion Sort
• This is an in-place comparison-based sorting algorithm.
• In this, a sub-list is maintained which is always sorted.
• For example, the lower part of an array is maintained to be sorted.
• An element which is to be 'inserted’ in this sorted sub-list, has to find
its appropriate place and then it has to be inserted there.
• Hence the name, insertion sort.
• Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Insertion Sort (Characteristics)
1. It is efficient for smaller data sets, but very inefficient for larger lists.
2. Insertion Sort is adaptive, that means it reduces its total number of
steps if a partially sorted array is provided as input, making it
efficient.
3. It is better than Selection Sort and Bubble Sort algorithms.
4. Its space complexity is less. Like bubble Sort, insertion sort also
requires a single additional memory space.
5. It is a stable sorting technique, as it does not change the relative
order of elements which are equal.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Insertion Sort (Algorithm)
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index
1, the key. The key element here is the new card that we need to add to our
existing sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case,
element at index 0:
o If the key element is less than the first element, we insert the key element before the first
element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Insertion Sort (Algorithm)
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index
1, the key. The key element here is the new card that we need to add to our
existing sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case,
element at index 0:
o If the key element is less than the first element, we insert the key element before the first
element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
5 1 6 2 4
Start with second
element as key
3
5 1 6 2 4 3
Reach the front
enter 1 here
1 < 5
1 5 6 2 4 3
6 > 1 6 > 5
No Change in order
1 5 6 2 4 3
2 > 1 2 < 5
Insert 2 before 5 and
after 1
2 < 6
Insertion Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
1 2 5 6 4 3
4 > 1 4 < 5
Insert 4 before 5 and
after 2
4 < 6
4 > 2
1 2 4 5 6 3
3 > 1 3 < 5
Insert 3 before 4 and
after 2
3 < 6
3 > 2 3 < 4
1 2 3 4 5 6 Sorted Array
Insertion Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Insertion Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒎𝒂𝒕𝒉. 𝒉 >
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐.𝒉 >
/∗ 𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚 𝒖𝒔𝒊𝒏𝒈 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/
𝒗𝒐𝒊𝒅 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏)
{
𝒊𝒏𝒕 𝒊, 𝒌𝒆𝒚,𝒋;
𝒇𝒐𝒓 𝒊 = 𝟏; 𝒊 < 𝒏; 𝒊 + +
{
𝒌𝒆𝒚 = 𝒂𝒓𝒓[𝒊];
𝒋 = 𝒊 − 𝟏;
/∗ 𝑴𝒐𝒗𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒐𝒇 𝒂𝒓𝒓[𝟎. . 𝒊 − 𝟏], 𝒕𝒉𝒂𝒕 𝒂𝒓𝒆 𝒈𝒓𝒆𝒂𝒕𝒆𝒓 𝒕𝒉𝒂𝒏 𝒌𝒆𝒚, 𝒕𝒐 𝒐𝒏𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 𝒂𝒉𝒆𝒂𝒅 𝒐𝒇 𝒕𝒉𝒆𝒊𝒓 𝒄𝒖𝒓𝒓𝒆𝒏𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 ∗/
𝒘𝒉𝒊𝒍𝒆 𝒋 >= 𝟎 && 𝒂𝒓𝒓 𝒋 > 𝒌𝒆𝒚
{
𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒂𝒓𝒓[𝒋];
𝒋 = 𝒋 − 𝟏;
}
𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒌𝒆𝒚;
}
}
Insertion Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
// 𝑨 𝒖𝒕𝒊𝒍𝒊𝒕𝒚 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒑𝒓𝒊𝒏𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚 𝒐𝒇 𝒔𝒊𝒛𝒆 𝒏
𝒗𝒐𝒊𝒅 𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏)
{
𝒊𝒏𝒕 𝒊;
𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓[𝒊]);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
}
/∗ 𝑫𝒓𝒊𝒗𝒆𝒓 𝒑𝒓𝒐𝒈𝒓𝒂𝒎 𝒕𝒐 𝒕𝒆𝒔𝒕 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/
𝒊𝒏𝒕 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒂𝒓𝒓[] = { 𝟏𝟐, 𝟏𝟏, 𝟏𝟑, 𝟓, 𝟔 };
𝒊𝒏𝒕 𝒏 = 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓) / 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓[𝟎]);
𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝒏);
𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒂𝒓𝒓, 𝒏);
𝒓𝒆𝒕𝒖𝒓𝒏 𝟎;
}
Quick Sort
• Quick Sort is a Divide and Conquer algorithm.
• It picks an element as pivot and partitions the given array around the
picked pivot.
• There are many different versions of Quick Sort that pick pivot in different
ways.
o Always pick first element as pivot.
o Always pick last element as pivot
o Pick a random element as pivot.
o Pick median as pivot.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Quick Sort (Algorithm)
Following are the steps involved in quick sort algorithm:
• After selecting an element as pivot, which is the last index of the array in
our case, we divide the array for the first time.
• In quick sort, the array elements are so positioned that all the elements
smaller than the pivot will be on the left side of the pivot and all the
elements greater than the pivot will be on the right side of it.
• And the pivot element will be at its final sorted position.
• The elements to the left and right, may not be sorted.
• Then we pick subarrays, elements on the left of pivot and elements on the
right of pivot, and we perform partitioning on them by choosing a pivot in
the subarrays.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Quick Sort (Algorithm)
• Partition considers the array elements a[first] through a[last].
• All items with keys less than the pivot are moved to lower positions in
the array
• All items with keys greater than the pivot are moved to higher
positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Quick Sort (Algorithm)
• Partition considers the array elements a[first] through a[last].
• All items with keys less than the pivot are moved to lower positions in
the array
• All items with keys greater than the pivot are moved to higher
positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
All are ≤ 70 All are > 70
First Last
The Partition algorithm yields
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
The algorithm increments Leftpos until the item at that position is greater than or equal to the
pivot value
It decrements Rightpos until the item at that position is less than or equal to the pivot value
Then it swaps the values at those two positions
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
90 > 70 and 50 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
50 100 20 60 80 110 120 40 10 30 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
100 > 70 and 30 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
50 30 20 60 80 110 120 40 10 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
80 > 70 and 10 < 70 (swap)
increment Leftpos until a[Leftpos] >= pivot
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
50 30 20 60 10 110 120 40 80 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
110 > 70 and 40 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Rightpos
50 30 20 60 10 40 120 110 80 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos
Eventually this stops when the two variables cross paths
The Partition algorithm finishes by swapping the pivot at position last with the item at
Leftpos
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Rightpos
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Quicksort starts by partitioning the array. For the example above, the result of the first
partition is
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
All are ≤ 70 All are > 70
The pivot value, 70, holds the same position it would if the array were sorted.
All items to the left of it are smaller and all items to the right of it are larger.
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
• The pivot value, 70, holds the same position it would if the array were
sorted.
• All items to the left of it are smaller and all items to the right of it are
larger.
• Thus, the 70 is in the right spot and need not be considered further.
• Quicksort can continue by simply applying itself recursively to the two
segments of the array above and below the pivot.
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Quick Sort
10 30 20 40 50 60 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
10 20 30 40 50 60 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Quick Sort
10 20 30 40 50 60 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
10 20 30 40 50 60 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
10 20 30 40 50 60 70 80 90 100 110 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Quick Sort
10 20 30 40 50 60 70 80 90 100 110 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Sorted Array
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Quick Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
#𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 >
𝒊𝒏𝒕 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅);
𝒗𝒐𝒊𝒅 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅);
𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏()
{
𝒊𝒏𝒕 𝒊;
𝒊𝒏𝒕 𝒂𝒓𝒓[𝟏𝟎] = {𝟗𝟎, 𝟐𝟑, 𝟏𝟎𝟏, 𝟒𝟓, 𝟔𝟓, 𝟐𝟖, 𝟔𝟕, 𝟖𝟗, 𝟑𝟒, 𝟐𝟗};
𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝟎, 𝟗);
𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝒔𝒐𝒓𝒕𝒆𝒅 𝒂𝒓𝒓𝒂𝒚 𝒊𝒔: 𝒏");
𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝟏𝟎; 𝒊 + +)
𝒑𝒓𝒊𝒏𝒕𝒇(" %𝒅𝒕", 𝒂𝒓𝒓[𝒊]);
}
Quick Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒊𝒏𝒕 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅)
{
𝒊𝒏𝒕 𝒍𝒆𝒇𝒕, 𝒓𝒊𝒈𝒉𝒕, 𝒕𝒆𝒎𝒑, 𝒍𝒐𝒄, 𝒇𝒍𝒂𝒈;
𝒍𝒐𝒄 = 𝒍𝒆𝒇𝒕 = 𝒃𝒆𝒈;
𝒓𝒊𝒈𝒉𝒕 = 𝒆𝒏𝒅;
𝒇𝒍𝒂𝒈 = 𝟎;
𝒘𝒉𝒊𝒍𝒆(𝒇𝒍𝒂𝒈 ! = 𝟏)
{
𝒘𝒉𝒊𝒍𝒆((𝒂[𝒍𝒐𝒄] <= 𝒂[𝒓𝒊𝒈𝒉𝒕]) && (𝒍𝒐𝒄! = 𝒓𝒊𝒈𝒉𝒕))
𝒓𝒊𝒈𝒉𝒕 − −;
𝒊𝒇(𝒍𝒐𝒄 == 𝒓𝒊𝒈𝒉𝒕)
𝒇𝒍𝒂𝒈 = 𝟏;
𝒆𝒍𝒔𝒆 𝒊𝒇(𝒂[𝒍𝒐𝒄] > 𝒂[𝒓𝒊𝒈𝒉𝒕])
{
𝒕𝒆𝒎𝒑 = 𝒂[𝒍𝒐𝒄];
𝒂[𝒍𝒐𝒄] = 𝒂[𝒓𝒊𝒈𝒉𝒕];
𝒂[𝒓𝒊𝒈𝒉𝒕] = 𝒕𝒆𝒎𝒑;
𝒍𝒐𝒄 = 𝒓𝒊𝒈𝒉𝒕;
}
Quick Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒊𝒇(𝒇𝒍𝒂𝒈! = 𝟏)
{
𝒘𝒉𝒊𝒍𝒆((𝒂[𝒍𝒐𝒄] >= 𝒂[𝒍𝒆𝒇𝒕]) && (𝒍𝒐𝒄! = 𝒍𝒆𝒇𝒕))
𝒍𝒆𝒇𝒕 + +;
𝒊𝒇(𝒍𝒐𝒄 == 𝒍𝒆𝒇𝒕)
𝒇𝒍𝒂𝒈 = 𝟏;
𝒆𝒍𝒔𝒆 𝒊𝒇(𝒂[𝒍𝒐𝒄] < 𝒂[𝒍𝒆𝒇𝒕])
{
𝒕𝒆𝒎𝒑 = 𝒂[𝒍𝒐𝒄];
𝒂[𝒍𝒐𝒄] = 𝒂[𝒍𝒆𝒇𝒕];
𝒂[𝒍𝒆𝒇𝒕] = 𝒕𝒆𝒎𝒑;
𝒍𝒐𝒄 = 𝒍𝒆𝒇𝒕;
}
}
}
𝒓𝒆𝒕𝒖𝒓𝒏 𝒍𝒐𝒄;
}
Quick Sort (Program)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒗𝒐𝒊𝒅 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅)
{
𝒊𝒏𝒕 𝒍𝒐𝒄;
𝒊𝒇(𝒃𝒆𝒈 < 𝒆𝒏𝒅)
{
𝒍𝒐𝒄 = 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒂, 𝒃𝒆𝒈, 𝒆𝒏𝒅);
𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂, 𝒃𝒆𝒈, 𝒍𝒐𝒄 − 𝟏);
𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂, 𝒍𝒐𝒄 + 𝟏, 𝒆𝒏𝒅);
}
}
Radix Sort
• Radix sort is one of the sorting algorithms used to sort a list of integer
numbers in order.
• In radix sort algorithm, a list of integer numbers will be sorted based on
the digits of individual numbers.
• Sorting is performed from least significant digit to the most significant
digit.
• Radix sort algorithm requires the number of passes which are equal to
the number of digits present in the largest number among the list of
numbers.
• For example, if the largest number is a 3 digit number then that list is
sorted with 3 passes.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Radix Sort (Algorithm)
The Radix sort algorithm is performed using the following steps...
• Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
• Step 2 - Consider the least significant digit of each number in the list which is to
be sorted.
• Step 3 - Insert each number into their respective queue based on the least
significant digit.
• Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have
inserted into their respective queues.
• Step 5 - Repeat from step 3 based on the next least significant digit.
• Step 6 - Repeat from step 2 until all the numbers are grouped based on the most
significant digit.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Radix Sort (Algorithm)
Example
Consider the following list of unsorted integer numbers
82, 901, 100, 12, 150, 77, 55, 23
Step 1: Define 10 queues each represents a bucket for digit from 0 to 9
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
queue 0 1 2 3 4 5 6 7 8 9
Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the Least
Significant Digit (once placed digit) of every number
82, 901, 100, 12, 150, 77, 55, 23
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
150
100 901
12
82 23 55 77
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 150, 901, 82, 12, 23, 55, 77
Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the next
Least Significant Digit (tens placed digit) of every number
100, 150, 901, 82, 12, 23, 55, 77
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
901
100 12 23
55
150 77 82
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 901, 12, 23, 150, 55, 77, 82
Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the next
Least Significant Digit (hundreds placed digit) of every number
100, 901, 12, 23, 150, 55, 77, 82
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
82
77
55
23
12
150
100 901
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
12, 23, 55, 77, 82, 100, 150, 901
List got sorted in the increasing order
Radix Sort (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
Radix Sort (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒊𝒇 = 𝒎𝒂𝒙 (((𝒂𝒓𝒓𝒂𝒚[𝒊] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎) > 𝒎𝒂𝒙){𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟏; 𝒊 < 𝒔𝒊𝒛𝒆; 𝒊 + +) 𝒊𝒏𝒕 𝒎𝒂𝒙
= (𝒂𝒓𝒓𝒂𝒚[𝟎] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎; 𝒊𝒏𝒕 𝒐𝒖𝒕𝒑𝒖𝒕[𝒔𝒊𝒛𝒆
+ 𝟏]; {𝒗𝒐𝒊𝒅 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆, 𝒊𝒏𝒕 𝒑𝒍𝒂𝒄𝒆) /
/ 𝑼𝒔𝒊𝒏𝒈 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈 𝒔𝒐𝒓𝒕 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝒕𝒉𝒆 𝒃𝒂𝒔𝒊𝒔 𝒐𝒇 𝒔𝒊𝒈𝒏𝒊𝒇𝒊𝒄𝒂𝒏𝒕 𝒑𝒍𝒂𝒄𝒆𝒔
𝒄𝒐𝒖𝒏𝒕[(𝒂𝒓𝒓𝒂𝒚[𝒊] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎] + +; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟎; 𝒊 < 𝒔𝒊𝒛𝒆; 𝒊 + +)// 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆 𝒄𝒐𝒖𝒏𝒕 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔𝒄𝒐𝒖𝒏𝒕[𝒊]
= 𝟎; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟎; 𝒊 < 𝒎𝒂𝒙; + +𝒊)𝒊𝒏𝒕 𝒄𝒐𝒖𝒏𝒕[𝒎𝒂𝒙 + 𝟏]; }𝒂𝒓𝒓𝒂𝒚[𝒊];
Radix Sort (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
𝒄𝒐𝒖𝒏𝒕 𝒊 += 𝒄𝒐𝒖𝒏𝒕 𝒊 − 𝟏 ; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟏; 𝒊 < 𝟏𝟎; 𝒊 + +)// 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆 𝒄𝒖𝒎𝒎𝒖𝒍𝒂𝒕𝒊𝒗𝒆 𝒄𝒐𝒖𝒏𝒕
Radix Sort (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
}𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈𝑺𝒐𝒓𝒕(𝒂𝒓𝒓𝒂𝒚, 𝒔𝒊𝒛𝒆, 𝒑𝒍𝒂𝒄𝒆); 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒑𝒍𝒂𝒄𝒆 = 𝟏; 𝒎𝒂𝒙 / 𝒑𝒍𝒂𝒄𝒆 > 𝟎; 𝒑𝒍𝒂𝒄𝒆 ∗
= 𝟏𝟎)// 𝑨𝒑𝒑𝒍𝒚 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈 𝒔𝒐𝒓𝒕 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒃𝒂𝒔𝒆𝒅 𝒐𝒏 𝒑𝒍𝒂𝒄𝒆 𝒗𝒂𝒍𝒖𝒆. 𝒊𝒏𝒕 𝒎𝒂𝒙
= 𝒈𝒆𝒕𝑴𝒂𝒙(𝒂𝒓𝒓𝒂𝒚, 𝒔𝒊𝒛𝒆);// 𝑮𝒆𝒕 𝒎𝒂𝒙𝒊𝒎𝒖𝒎 𝒆𝒍𝒆𝒎𝒆𝒏𝒕{𝒗𝒐𝒊𝒅 𝒓𝒂𝒅𝒊𝒙𝒔𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆) /
/ 𝑴𝒂𝒊𝒏 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒊𝒎𝒑𝒍𝒆𝒎𝒆𝒏𝒕 𝒓𝒂𝒅𝒊𝒙 𝒔𝒐𝒓𝒕
Radix Sort (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
}𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒂𝒓𝒓𝒂𝒚, 𝒏); 𝒓𝒂𝒅𝒊𝒙𝒔𝒐𝒓𝒕(𝒂𝒓𝒓𝒂𝒚, 𝒏); 𝒊𝒏𝒕 𝒏 = 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓𝒂𝒚) / 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓𝒂𝒚[𝟎]); 𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[]
= {𝟏𝟐𝟏, 𝟒𝟑𝟐, 𝟓𝟔𝟒, 𝟐𝟑, 𝟏, 𝟒𝟓, 𝟕𝟖𝟖}; {𝒊𝒏𝒕 𝒎𝒂𝒊𝒏() // 𝑫𝒓𝒊𝒗𝒆𝒓 𝒄𝒐𝒅𝒆}𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); }𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓𝒂𝒚[𝒊]); {𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊
= 𝟎; 𝒊 < 𝒔𝒊𝒛𝒆; + +𝒊) {𝒗𝒐𝒊𝒅 𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆) // 𝑷𝒓𝒊𝒏𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚

More Related Content

Similar to Unit 2 Searching and Sorting Technique.pptx

IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
ASMAALWADEE2
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
Misssaxena
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
chauhankapil
 
Linkedlist with each and every step Single. doubly
Linkedlist with each and every step Single. doublyLinkedlist with each and every step Single. doubly
Linkedlist with each and every step Single. doubly
akshmineshpatel
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
An Introduction to Reinforcement Learning - The Doors to AGI
An Introduction to Reinforcement Learning - The Doors to AGIAn Introduction to Reinforcement Learning - The Doors to AGI
An Introduction to Reinforcement Learning - The Doors to AGI
Anirban Santara
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
SylrizcinMarieManzo3
 
Binary search
Binary search Binary search
Binary search
Ramkrishna bhagat
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
Jabs6
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort
Terry Yoast
 
9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort
Terry Yoast
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
Krishnanandmishra15
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
Maulana Abul Kalam Azad University of Technology
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING

Similar to Unit 2 Searching and Sorting Technique.pptx (20)

IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Chapter3.pptx
Chapter3.pptxChapter3.pptx
Chapter3.pptx
 
Searching techniques with progrms
Searching techniques with progrmsSearching techniques with progrms
Searching techniques with progrms
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
 
Linear search in ds
Linear search in dsLinear search in ds
Linear search in ds
 
Linkedlist with each and every step Single. doubly
Linkedlist with each and every step Single. doublyLinkedlist with each and every step Single. doubly
Linkedlist with each and every step Single. doubly
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
An Introduction to Reinforcement Learning - The Doors to AGI
An Introduction to Reinforcement Learning - The Doors to AGIAn Introduction to Reinforcement Learning - The Doors to AGI
An Introduction to Reinforcement Learning - The Doors to AGI
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Binary search
Binary search Binary search
Binary search
 
Data Structures in C
Data Structures in CData Structures in C
Data Structures in C
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort
 
9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort9781111530532 ppt ch14_quick_sort
9781111530532 ppt ch14_quick_sort
 
Presentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptxPresentation on Searching and Sorting in C language.pptx
Presentation on Searching and Sorting in C language.pptx
 
Analysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptxAnalysis of Algorithm - Binary Search.pptx
Analysis of Algorithm - Binary Search.pptx
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 

More from Vaibhav Parjane

PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
unit 1 SIXTH.pptx Algorithm Complexity Time
unit 1 SIXTH.pptx Algorithm Complexity Timeunit 1 SIXTH.pptx Algorithm Complexity Time
unit 1 SIXTH.pptx Algorithm Complexity Time
Vaibhav Parjane
 
Operations on Data Structure- Several Operation performed on DS
Operations on Data Structure- Several Operation performed on DSOperations on Data Structure- Several Operation performed on DS
Operations on Data Structure- Several Operation performed on DS
Vaibhav Parjane
 
Classification of Data Structure -Linear and Non Linear
Classification of Data Structure -Linear and Non LinearClassification of Data Structure -Linear and Non Linear
Classification of Data Structure -Linear and Non Linear
Vaibhav Parjane
 
Need of Data Structure & Abstract Data Type
Need of Data Structure & Abstract Data TypeNeed of Data Structure & Abstract Data Type
Need of Data Structure & Abstract Data Type
Vaibhav Parjane
 
Introduction to data & Data Structure Definition
Introduction to data & Data Structure DefinitionIntroduction to data & Data Structure Definition
Introduction to data & Data Structure Definition
Vaibhav Parjane
 

More from Vaibhav Parjane (6)

PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
 
unit 1 SIXTH.pptx Algorithm Complexity Time
unit 1 SIXTH.pptx Algorithm Complexity Timeunit 1 SIXTH.pptx Algorithm Complexity Time
unit 1 SIXTH.pptx Algorithm Complexity Time
 
Operations on Data Structure- Several Operation performed on DS
Operations on Data Structure- Several Operation performed on DSOperations on Data Structure- Several Operation performed on DS
Operations on Data Structure- Several Operation performed on DS
 
Classification of Data Structure -Linear and Non Linear
Classification of Data Structure -Linear and Non LinearClassification of Data Structure -Linear and Non Linear
Classification of Data Structure -Linear and Non Linear
 
Need of Data Structure & Abstract Data Type
Need of Data Structure & Abstract Data TypeNeed of Data Structure & Abstract Data Type
Need of Data Structure & Abstract Data Type
 
Introduction to data & Data Structure Definition
Introduction to data & Data Structure DefinitionIntroduction to data & Data Structure Definition
Introduction to data & Data Structure Definition
 

Recently uploaded

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
ClaraZara1
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
yokeleetan1
 

Recently uploaded (20)

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)6th International Conference on Machine Learning & Applications (CMLA 2024)
6th International Conference on Machine Learning & Applications (CMLA 2024)
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
Swimming pool mechanical components design.pptx
Swimming pool  mechanical components design.pptxSwimming pool  mechanical components design.pptx
Swimming pool mechanical components design.pptx
 

Unit 2 Searching and Sorting Technique.pptx

  • 1. SANJIVANI K. B. P. POLYTECHNIC, KOPARGAON With NBA ACCREDIATED programs , Approved by AICTE, New Delhi, Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai, ISO 9001:2015 Certified Institute Department:- Computer Technology Class:- CM3I Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317 Name of Faculty: Prof. Vaibhav A. Parjane
  • 2. Searching and Sorting Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 3. Unit Outcome After going through this unit, the student will be able to: 2a. Explain working of the given search method with an example. 2b. Write an algorithm to search the given key using binary Search method 2c. Write an Algorithm to sort data using a specified sorting method. 2d. Explain the working of given sorting method step by-step with an example and small data set. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 4. Introduction: (Searching) • Searching: Searching is a process of finding the location of a particular element in an array is called searching. • There are two types of searching:  Linear Search:  Binary Search: Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 5. Linear Search • Linear search or sequential search is a method for finding a particular value in a list that consists of checking every one of its elements • One element at a time and in sequence, until the desired one is found. • It is simplest search algorithm Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 6. Linear Search • Consider an array of 20 elements. • Suppose, element 8 is to be search in the array. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 7. Linear Search (Algorithm) Let us start with an array or list, L which may have the item in question. Step 1: If the list L is empty, then the list has nothing. The list does not have the item in question. Stop here. Step 2: Otherwise, look at all the elements in the list L. Step 3: For each element: If the element equals the item in question, the list contains the item in question. Stop here. Otherwise, go onto next element. Step 4: The list does not have the item in question. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 8. Linear Search (Program ) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane # 𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 > 𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂𝒓𝒓[𝟓𝟎], 𝒏, 𝒊, 𝒊𝒕𝒆𝒎; 𝒑𝒓𝒊𝒏𝒕𝒇("𝑯𝒐𝒘 𝒎𝒂𝒏𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒚𝒐𝒖 𝒘𝒂𝒏𝒕 𝒕𝒐 𝒆𝒏𝒕𝒆𝒓 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚 ∶ "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏); 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊 + 𝟏); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]); } 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 ∶ "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒊𝒕𝒆𝒎);
  • 9. Linear Search (Program ) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒇𝒐𝒓( 𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { // 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 𝒊𝒕𝒆𝒎 𝒊𝒔 𝒄𝒐𝒎𝒑𝒂𝒓𝒆𝒅 𝒘𝒊𝒕𝒉 𝒂𝒓𝒓𝒂𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒊𝒇 𝒊𝒕𝒆𝒎 == 𝒂𝒓𝒓 𝒊 { 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒇𝒐𝒖𝒏𝒅 𝒂𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 %𝒅𝒏", 𝒊𝒕𝒆𝒎, 𝒊 + 𝟏); 𝒃𝒓𝒆𝒂𝒌; } } 𝒊𝒇(𝒊 == 𝒏) 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑰𝒕𝒆𝒎 %𝒅 𝒏𝒐𝒕 𝒇𝒐𝒖𝒏𝒅 𝒊𝒏 𝒂𝒓𝒓𝒂𝒚𝒏", 𝒊𝒕𝒆𝒎); 𝒈𝒆𝒕𝒄𝒉(); } Continued……
  • 10. Binary Search • Also known as half-interval search algorithm. • Finds the position of a specified input value within an array sorted by key value. • For binary search, the array should be arranged in ascending or descending order. • The binary search is based on the divide-and-conquer approach. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 11. Binary Search • In this technique, the element to be searched (say, item) is compared with the middle element of the array. • If item is equal to the middle element, then search is successful. • If the item is smaller than the middle element, then item is searched in the segment of the array before the middle element. If the item is greater than the middle element, item is searched in the array segment after the middle element. • This process will be in iteration until the element is found or the array segment is reduced to a single element that is not equal to item. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 12. Binary Search • Consider an sorted array of 11 elements as shown in figure below. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane Suppose we want to search the element 55 from the array of elements.
  • 13. Binary Search • For this we will take 3 variables Start, End and Middle • Which will keep track of the status of start, end and middle value of the portion of the array, in which we will search the element. • The value of the middle will be as: Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2 • Initially, 𝑺𝒕𝒂𝒓𝒕 = 𝟏 𝑬𝒏𝒅 = 𝟏𝟏 and 𝑴𝒊𝒅𝒅𝒍𝒆 = (𝟏 + 𝟏𝟏) / 𝟐 = 𝟔. • The key element 55 is to be compared with the Middle value. • The value at index 6 is 48 and it is smaller than the target value i.e. (55).
  • 14. Binary Search • If the key is less than the value at Middle then the key element is present in the first half else in other half; • In our case, the key is on the right half. • Hence, now Start = Middle + 1 = 6 + 1 =7. • Calculate the middle index of the second half Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2 𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 11 2 𝑴𝒊𝒅𝒅𝒍𝒆 = 9
  • 15. Binary Search • Again, the Middle divides the second half into the two parts as shown in fig below Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane • The key element 55 is lesser than the value at Middle which is 72 • Hence it is present in the left half i.e., towards the left of 72. • Hence now, End = Middle – 1 = 8. Start will remain 7
  • 16. Binary Search • At last, the Middle is calculated as Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane • Now if key element 55 is compared with the value at middle, then it is found that they are equal. • The key element is searched successfully and it is in 7th location 𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2 𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 8 2 𝑴𝒊𝒅𝒅𝒍𝒆 = 7
  • 17. Binary Search (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 1.Start with the middle element: •If the target value is equal to the middle element of the array, then return the index of the middle element. •If not, then compare the middle element with the target value, •If the target value is greater than the number in the middle index, then pick the elements to the right of the middle index, and start with Step 1. •If the target value is less than the number in the middle index, then pick the elements to the left of the middle index, and start with Step 1. 2.When a match is found, return the index of the element matched. 3.If no match is found, then return -1
  • 18. Binary Search (Program ) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 > 𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂𝒓𝒓[𝟐𝟎], 𝒔𝒕𝒂𝒓𝒕, 𝒆𝒏𝒅, 𝒎𝒊𝒅𝒅𝒍𝒆, 𝒏, 𝒊, 𝒊𝒕𝒆𝒎; 𝒄𝒍𝒓𝒔𝒄𝒓(); 𝒑𝒓𝒊𝒏𝒕𝒇("𝑯𝒐𝒘 𝒎𝒂𝒏𝒚 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒚𝒐𝒖 𝒘𝒂𝒏𝒕 𝒕𝒐 𝒆𝒏𝒕𝒆𝒓 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚 ∶ "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏); 𝒇𝒐𝒓(𝒊 = 𝟏; 𝒊 <= 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]); } 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 ∶ "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒊𝒕𝒆𝒎); // 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒕𝒐 𝒃𝒆 𝒔𝒆𝒂𝒓𝒄𝒉𝒆𝒅 𝒊𝒔 𝒔𝒕𝒐𝒓𝒆𝒅 𝒊𝒏 𝒗𝒂𝒓𝒊𝒂𝒃𝒍𝒆 𝒊𝒕𝒆𝒎
  • 19. Binary Search (Program ) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒔𝒕𝒂𝒓𝒕 = 𝟏; 𝒆𝒏𝒅 = 𝒏; 𝒎𝒊𝒅𝒅𝒍𝒆 = (𝒔𝒕𝒂𝒓𝒕 + 𝒆𝒏𝒅) / 𝟐; 𝒘𝒉𝒊𝒍𝒆(𝒊𝒕𝒆𝒎! = 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆] && 𝒔𝒕𝒂𝒓𝒕 <= 𝒆𝒏𝒅) { 𝒊𝒇(𝒊𝒕𝒆𝒎 > 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆]) 𝒔𝒕𝒂𝒓𝒕 = 𝒎𝒊𝒅𝒅𝒍𝒆 + 𝟏; 𝒆𝒍𝒔𝒆 𝒆𝒏𝒅 = 𝒎𝒊𝒅𝒅𝒍𝒆 − 𝟏; 𝒎𝒊𝒅𝒅𝒍𝒆 = (𝒔𝒕𝒂𝒓𝒕 + 𝒆𝒏𝒅) / 𝟐; } 𝒊𝒇(𝒊𝒕𝒆𝒎 == 𝒂𝒓𝒓[𝒎𝒊𝒅𝒅𝒍𝒆]) 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒊𝒔 𝒇𝒐𝒖𝒏𝒅 𝒂𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 %𝒅𝒏", 𝒊𝒕𝒆𝒎, 𝒎𝒊𝒅𝒅𝒍𝒆); 𝒊𝒇(𝒔𝒕𝒂𝒓𝒕 > 𝒆𝒏𝒅) 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏%𝒅 𝒊𝒔 𝒏𝒐𝒕 𝒇𝒐𝒖𝒏𝒅 𝒊𝒏 𝒂𝒓𝒓𝒂𝒚𝒏", 𝒊𝒕𝒆𝒎); 𝒈𝒆𝒕𝒄𝒉(); }
  • 20. Introduction (Sorting) • Sorting: Sorting refers to arranging data in a particular format. • Sorting means arranging the data in ascending or descending order. • There are the several sorting techniques:  Bubble Sort  Selection Sort  Insertion Sort  Quick Sort  Radix Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 21. Bubble Sort (Algorithm) Following are the steps involved in bubble sort(for sorting a given array in ascending order): 1. Starting with the first element(index = 0), compare the current element with the next element of the array. 2. If the current element is greater than the next element of the array, swap them. 3. If the current element is less than the next element, move to the next element. Repeat Step1. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 22. Bubble Sort Let's consider an array with values {5, 1, 6, 2, 4, 3} Next, we have a pictorial representation of how bubble sort will sort the given array. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 23. 5 1 6 2 4 3 5 1 6 2 4 3 1 5 6 2 4 3 1 5 2 6 4 3 1 5 4 2 6 3 1 5 2 4 3 6 Iteration 1 5 > 1 Interchange 5 < 6 No change 6 > 2 Interchange 6 > 4 Interchange 6 > 3 Interchange Bubble Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 24. 2 1 5 4 3 6 1 2 4 5 3 6 Iteration 2 5 > 2 Interchange 5 > 4 Interchange 5 > 3 Interchange 1 5 2 4 3 6 1 2 4 3 5 6 1 < 5 No change 1 5 2 4 3 6 Bubble Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 25. Iteration 3 2 < 4 No change 4 > 3 Interchange 1 2 4 3 5 6 1 < 2 No change 1 2 4 3 5 6 1 2 4 3 5 6 1 2 3 4 5 6 After Iteration 5 we get all elements sorted 1 2 3 4 5 6 Bubble Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 26. Bubble Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒓𝒊𝒏𝒈. 𝒉 > 𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂[𝟏𝟎], 𝒊, , 𝒋, 𝒌, 𝒕𝒆𝒎𝒑, 𝒏, 𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔; 𝒄𝒍𝒓𝒔𝒄𝒓(); 𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 ∶ 𝒎𝒂𝒙 𝟐𝟓"); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ ", 𝒊 + 𝟏); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂[𝒊]); } 𝒑𝒓𝒊𝒏𝒕𝒇("𝑼𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏"); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏");
  • 27. Bubble Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane /∗ 𝑩𝒖𝒃𝒃𝒍𝒆 𝒔𝒐𝒓𝒕 ∗/ 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏 ; 𝒊 + +) { 𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 = 𝟎; 𝒇𝒐𝒓 (𝒋 = 𝟎; 𝒋 < 𝒏 − 𝟏 − 𝒊; 𝒋 + +) { 𝒊𝒇 (𝒂[𝒋] > 𝒂[𝒋 + 𝟏]) { 𝒕𝒆𝒎𝒑 = 𝒂[𝒋]; 𝒂[𝒋] = 𝒂[𝒋 + 𝟏]; 𝒂[𝒋 + 𝟏] = 𝒕𝒆𝒎𝒑; 𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 + +; } /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒊𝒇 ∗/ } /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒇𝒐𝒓 𝒍𝒐𝒐𝒑 ∗/ 𝒊𝒇(𝒙𝒄𝒉𝒂𝒏𝒈𝒆𝒔 == 𝟎) /∗ 𝑰𝒇 𝒍𝒊𝒔𝒕 𝒊𝒔 𝒔𝒐𝒓𝒕𝒆𝒅 ∗/ 𝒃𝒓𝒆𝒂𝒌; 𝒑𝒓𝒊𝒏𝒕𝒇("𝑨𝒇𝒕𝒆𝒓 𝑷𝒂𝒔𝒔 %𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒂𝒓𝒆 ∶ ", 𝒊 + 𝟏); 𝒇𝒐𝒓 (𝒌 = 𝟎; 𝒌 < 𝒏; 𝒌 + +) 𝒑𝒓𝒊𝒏𝒕𝒇 %d , 𝒂 𝒌 ; 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); } /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒐𝒖𝒕𝒆𝒓 𝒇𝒐𝒓 𝒍𝒐𝒐𝒑 ∗/
  • 28. Bubble Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏"); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]); 𝒈𝒆𝒕𝒄𝒉(); }
  • 29. Bubble Sort (Program using function) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > 𝒗𝒐𝒊𝒅 𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏) { 𝒊𝒏𝒕 𝒊, 𝒋, 𝒕𝒆𝒎𝒑; 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒇𝒐𝒓(𝒋 = 𝟎; 𝒋 < 𝒏 − 𝒊 − 𝟏; 𝒋 + +) { // 𝒇𝒍𝒂𝒈 𝒕𝒐 𝒎𝒐𝒏𝒊𝒕𝒐𝒓 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈 𝒊𝒏𝒕 𝒇𝒍𝒂𝒈 = 𝟎; 𝒊𝒇( 𝒂𝒓𝒓[𝒋] > 𝒂𝒓𝒓[𝒋 + 𝟏]) { // 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈 𝒕𝒆𝒎𝒑 = 𝒂𝒓𝒓[𝒋]; 𝒂𝒓𝒓[𝒋] = 𝒂𝒓𝒓[𝒋 + 𝟏]; 𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒕𝒆𝒎𝒑; // 𝒊𝒇 𝒔𝒘𝒂𝒑𝒑𝒊𝒏𝒈 𝒉𝒂𝒑𝒑𝒆𝒏𝒔 𝒖𝒑𝒅𝒂𝒕𝒆 𝒇𝒍𝒂𝒈 𝒕𝒐 𝟏 𝒇𝒍𝒂𝒈 = 𝟏; } }
  • 30. Bubble Sort (Program using function) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane // 𝒊𝒇 𝒗𝒂𝒍𝒖𝒆 𝒐𝒇 𝒇𝒍𝒂𝒈 𝒊𝒔 𝒛𝒆𝒓𝒐 𝒂𝒇𝒕𝒆𝒓 𝒂𝒍𝒍 𝒕𝒉𝒆 𝒊𝒕𝒆𝒓𝒂𝒕𝒊𝒐𝒏𝒔 𝒐𝒇 𝒊𝒏𝒏𝒆𝒓 𝒍𝒐𝒐𝒑 // 𝒕𝒉𝒆𝒏 𝒃𝒓𝒆𝒂𝒌 𝒐𝒖𝒕 𝒊𝒇(! 𝒇𝒍𝒂𝒈) { 𝒃𝒓𝒆𝒂𝒌; } } // 𝒑𝒓𝒊𝒏𝒕 𝒕𝒉𝒆 𝒔𝒐𝒓𝒕𝒆𝒅 𝒂𝒓𝒓𝒂𝒚 𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝑨𝒓𝒓𝒂𝒚: "); 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓[𝒊]); } }
  • 31. Bubble Sort (Program using function) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒊𝒏𝒕 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂𝒓𝒓[𝟏𝟎𝟎], 𝒊, 𝒏, 𝒔𝒕𝒆𝒑, 𝒕𝒆𝒎𝒑; // 𝒂𝒔𝒌 𝒖𝒔𝒆𝒓 𝒇𝒐𝒓 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒕𝒐 𝒃𝒆 𝒔𝒐𝒓𝒕𝒆𝒅 𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒕𝒐 𝒃𝒆 𝒔𝒐𝒓𝒕𝒆𝒅: "); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒏); // 𝒊𝒏𝒑𝒖𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝒕𝒉𝒆 𝒂𝒓𝒓𝒂𝒚 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇("𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 𝒏𝒐. %𝒅: ", 𝒊 + 𝟏); 𝒔𝒄𝒂𝒏𝒇("%𝒅", &𝒂𝒓𝒓[𝒊]); } // 𝒄𝒂𝒍𝒍 𝒕𝒉𝒆 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕 𝒃𝒖𝒃𝒃𝒍𝒆𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝒏); }
  • 32. Selection Sort • Selection sort is conceptually the most simplest sorting algorithm. • This algorithm will first find the smallest element in the array and swap it with the element in the first position. • Then it will find the second smallest element and swap it with the element in the second position • It will repeat this until the entire array is sorted. • It is called selection sort because it repeatedly selects the next- smallest element and swaps it into the right place. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 33. Selection Sort (Algorithm) Following are the steps involved in selection sort(for sorting a given array in ascending order): Step 1 - Set MIN to location 0 Step 2 - Search the minimum element in the list Step 3 - Swap with value at location MIN Step 4 - Increment MIN to point to next element Step 5 - Repeat until list is sorted Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 34. Selection Sort (Algorithm) Following are the steps involved in selection sort(for sorting a given array in ascending order): 1. Set the first element as minimum 2. Compare minimum with the second element. 3. If the second element is smaller than minimum, assign the second element as minimum. 4. Compare minimum with the third element. 5. Again, if the third element is smaller, then assign minimum to the third element otherwise do nothing. 6. The process goes on until the last element. 7. After each iteration, minimum is placed in the front of the unsorted list. 8. For each iteration, indexing starts from the first unsorted element. 9. Step 1 to 3 are repeated until all the elements are placed at their correct positions. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 35. Step 1 21 11 8 13 1 21 11 8 13 1 21 11 8 13 1 21 11 8 13 1 1 11 8 13 21 Swapping 𝒊 = 𝟎 𝒊 = 𝟏 𝒊 = 𝟐 𝒊 = 𝟑 Min value at index 1 Min value at index 2 Min value at index 2 Min value at index 4 First Iteration Selection Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 36. Step 1 𝒊 = 𝟎 𝒊 = 𝟏 𝒊 = 𝟐 Min value at index 2 Min value at index 2 Min value at index 2 Second Iteration 1 11 8 13 21 1 11 8 13 21 1 11 8 13 21 1 8 11 13 21 Swapping Selection Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 37. Step 1 𝒊 = 𝟎 𝒊 = 𝟏 Min value at index 2 Min value at index 2 Already in Place Third Iteration 1 8 11 13 21 1 8 11 13 21 1 8 11 13 21 Selection Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 38. Step 1 𝒊 = 𝟎 𝒊 = 𝟏 Min value at index 3 Fourth Iteration 1 8 11 13 21 1 8 11 13 21 Selection Sort Already in Place Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 39. Selection Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒄𝒐𝒏𝒊𝒐. 𝒉 > 𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂[𝟐𝟓], 𝒊, , 𝒋, 𝒌, 𝒏, 𝒕𝒆𝒎𝒑, 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕; 𝒄𝒍𝒓𝒔𝒄𝒓(); 𝒑𝒓𝒊𝒏𝒕𝒇(“𝑬𝒏𝒕𝒆𝒓 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 ∶ “); 𝒔𝒄𝒂𝒏𝒇(“%𝒅”, &𝒏); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) { 𝒑𝒓𝒊𝒏𝒕𝒇(“𝑬𝒏𝒕𝒆𝒓 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 %𝒅 ∶ “, 𝒊 + 𝟏); 𝒔𝒄𝒂𝒏𝒇(“%𝒅”, &𝒂[𝒊]); } /∗ 𝑫𝒊𝒔𝒑𝒍𝒂𝒚 𝒕𝒉𝒆 𝒖𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 ∗/ 𝒑𝒓𝒊𝒏𝒕𝒇(“𝑼𝒏𝒔𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏”); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇(“%𝒅 “, 𝒂[𝒊]); 𝒑𝒓𝒊𝒏𝒕𝒇(“𝒏”);
  • 40. Selection Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane /∗ 𝑺𝒆𝒍𝒆𝒄𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/ 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝒏 − 𝟏 ; 𝒊 + +) { /∗ 𝑭𝒊𝒏𝒅 𝒕𝒉𝒆 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕 ∗/ 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 = 𝒊; 𝒇𝒐𝒓(𝒌 = 𝒊 + 𝟏; 𝒌 < 𝒏 ; 𝒌 + +) { 𝒊𝒇(𝒂[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕] > 𝒂[𝒌]) 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 = 𝒌 ; } 𝒊𝒇( 𝒊 ! = 𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕 ) { 𝒕𝒆𝒎𝒑 = 𝒂 [𝒊]; 𝒂[𝒊] = 𝒂[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕]; 𝒂𝒓𝒓[𝒔𝒎𝒂𝒍𝒍𝒆𝒔𝒕] = 𝒕𝒆𝒎𝒑 ; } 𝒑𝒓𝒊𝒏𝒕𝒇("𝑨𝒇𝒕𝒆𝒓 𝑷𝒂𝒔𝒔 %𝒅 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒂𝒓𝒆 ∶ ", 𝒊 + 𝟏); 𝒇𝒐𝒓 (𝒋 = 𝟎; 𝒋 < 𝒏; 𝒋 + +) 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒋]); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); } /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒇𝒐𝒓 ∗/
  • 41. Selection Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane /∗ 𝑺𝒆𝒍𝒆𝒄𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/ 𝒑𝒓𝒊𝒏𝒕𝒇("𝑺𝒐𝒓𝒕𝒆𝒅 𝒍𝒊𝒔𝒕 𝒊𝒔 ∶ 𝒏"); 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂[𝒊]); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); 𝒈𝒆𝒕𝒄𝒉(); } /∗ 𝑬𝒏𝒅 𝒐𝒇 𝒎𝒂𝒊𝒏() ∗/
  • 42. Insertion Sort • This is an in-place comparison-based sorting algorithm. • In this, a sub-list is maintained which is always sorted. • For example, the lower part of an array is maintained to be sorted. • An element which is to be 'inserted’ in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. • Hence the name, insertion sort. • Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 43. Insertion Sort (Characteristics) 1. It is efficient for smaller data sets, but very inefficient for larger lists. 2. Insertion Sort is adaptive, that means it reduces its total number of steps if a partially sorted array is provided as input, making it efficient. 3. It is better than Selection Sort and Bubble Sort algorithms. 4. Its space complexity is less. Like bubble Sort, insertion sort also requires a single additional memory space. 5. It is a stable sorting technique, as it does not change the relative order of elements which are equal. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 44. Insertion Sort (Algorithm) Following are the steps involved in insertion sort: 1. We start by making the second element of the given array, i.e. element at index 1, the key. The key element here is the new card that we need to add to our existing sorted set of cards(remember the example with cards above). 2. We compare the key element with the element(s) before it, in this case, element at index 0: o If the key element is less than the first element, we insert the key element before the first element. o If the key element is greater than the first element, then we insert it after the first element. 3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position. 4. And we go on repeating this, until the array is sorted. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 45. Insertion Sort (Algorithm) Following are the steps involved in insertion sort: 1. We start by making the second element of the given array, i.e. element at index 1, the key. The key element here is the new card that we need to add to our existing sorted set of cards(remember the example with cards above). 2. We compare the key element with the element(s) before it, in this case, element at index 0: o If the key element is less than the first element, we insert the key element before the first element. o If the key element is greater than the first element, then we insert it after the first element. 3. Then, we make the third element of the array as key and will compare it with elements to it's left and insert it at the right position. 4. And we go on repeating this, until the array is sorted. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 46. 5 1 6 2 4 Start with second element as key 3 5 1 6 2 4 3 Reach the front enter 1 here 1 < 5 1 5 6 2 4 3 6 > 1 6 > 5 No Change in order 1 5 6 2 4 3 2 > 1 2 < 5 Insert 2 before 5 and after 1 2 < 6 Insertion Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 47. 1 2 5 6 4 3 4 > 1 4 < 5 Insert 4 before 5 and after 2 4 < 6 4 > 2 1 2 4 5 6 3 3 > 1 3 < 5 Insert 3 before 4 and after 2 3 < 6 3 > 2 3 < 4 1 2 3 4 5 6 Sorted Array Insertion Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 48. Insertion Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒎𝒂𝒕𝒉. 𝒉 > #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐.𝒉 > /∗ 𝑭𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚 𝒖𝒔𝒊𝒏𝒈 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/ 𝒗𝒐𝒊𝒅 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏) { 𝒊𝒏𝒕 𝒊, 𝒌𝒆𝒚,𝒋; 𝒇𝒐𝒓 𝒊 = 𝟏; 𝒊 < 𝒏; 𝒊 + + { 𝒌𝒆𝒚 = 𝒂𝒓𝒓[𝒊]; 𝒋 = 𝒊 − 𝟏; /∗ 𝑴𝒐𝒗𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒐𝒇 𝒂𝒓𝒓[𝟎. . 𝒊 − 𝟏], 𝒕𝒉𝒂𝒕 𝒂𝒓𝒆 𝒈𝒓𝒆𝒂𝒕𝒆𝒓 𝒕𝒉𝒂𝒏 𝒌𝒆𝒚, 𝒕𝒐 𝒐𝒏𝒆 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 𝒂𝒉𝒆𝒂𝒅 𝒐𝒇 𝒕𝒉𝒆𝒊𝒓 𝒄𝒖𝒓𝒓𝒆𝒏𝒕 𝒑𝒐𝒔𝒊𝒕𝒊𝒐𝒏 ∗/ 𝒘𝒉𝒊𝒍𝒆 𝒋 >= 𝟎 && 𝒂𝒓𝒓 𝒋 > 𝒌𝒆𝒚 { 𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒂𝒓𝒓[𝒋]; 𝒋 = 𝒋 − 𝟏; } 𝒂𝒓𝒓[𝒋 + 𝟏] = 𝒌𝒆𝒚; } }
  • 49. Insertion Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane // 𝑨 𝒖𝒕𝒊𝒍𝒊𝒕𝒚 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒑𝒓𝒊𝒏𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚 𝒐𝒇 𝒔𝒊𝒛𝒆 𝒏 𝒗𝒐𝒊𝒅 𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒊𝒏𝒕 𝒂𝒓𝒓[], 𝒊𝒏𝒕 𝒏) { 𝒊𝒏𝒕 𝒊; 𝒇𝒐𝒓 (𝒊 = 𝟎; 𝒊 < 𝒏; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓[𝒊]); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); } /∗ 𝑫𝒓𝒊𝒗𝒆𝒓 𝒑𝒓𝒐𝒈𝒓𝒂𝒎 𝒕𝒐 𝒕𝒆𝒔𝒕 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏 𝒔𝒐𝒓𝒕 ∗/ 𝒊𝒏𝒕 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒂𝒓𝒓[] = { 𝟏𝟐, 𝟏𝟏, 𝟏𝟑, 𝟓, 𝟔 }; 𝒊𝒏𝒕 𝒏 = 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓) / 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓[𝟎]); 𝒊𝒏𝒔𝒆𝒓𝒕𝒊𝒐𝒏𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝒏); 𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒂𝒓𝒓, 𝒏); 𝒓𝒆𝒕𝒖𝒓𝒏 𝟎; }
  • 50. Quick Sort • Quick Sort is a Divide and Conquer algorithm. • It picks an element as pivot and partitions the given array around the picked pivot. • There are many different versions of Quick Sort that pick pivot in different ways. o Always pick first element as pivot. o Always pick last element as pivot o Pick a random element as pivot. o Pick median as pivot. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 51. Quick Sort (Algorithm) Following are the steps involved in quick sort algorithm: • After selecting an element as pivot, which is the last index of the array in our case, we divide the array for the first time. • In quick sort, the array elements are so positioned that all the elements smaller than the pivot will be on the left side of the pivot and all the elements greater than the pivot will be on the right side of it. • And the pivot element will be at its final sorted position. • The elements to the left and right, may not be sorted. • Then we pick subarrays, elements on the left of pivot and elements on the right of pivot, and we perform partitioning on them by choosing a pivot in the subarrays. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 52. Quick Sort (Algorithm) • Partition considers the array elements a[first] through a[last]. • All items with keys less than the pivot are moved to lower positions in the array • All items with keys greater than the pivot are moved to higher positions. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 53. Quick Sort (Algorithm) • Partition considers the array elements a[first] through a[last]. • All items with keys less than the pivot are moved to lower positions in the array • All items with keys greater than the pivot are moved to higher positions. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 54. 90 100 20 60 80 110 120 40 10 30 50 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 50 30 20 60 10 40 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot All are ≤ 70 All are > 70 First Last The Partition algorithm yields Quick Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 55. 90 100 20 60 80 110 120 40 10 30 50 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Rightpos The algorithm increments Leftpos until the item at that position is greater than or equal to the pivot value It decrements Rightpos until the item at that position is less than or equal to the pivot value Then it swaps the values at those two positions Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 56. 90 100 20 60 80 110 120 40 10 30 50 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Rightpos 90 > 70 and 50 < 70 (swap) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 57. 50 100 20 60 80 110 120 40 10 30 90 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Rightpos 100 > 70 and 30 < 70 (swap) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 58. 50 30 20 60 80 110 120 40 10 100 90 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Rightpos 80 > 70 and 10 < 70 (swap) increment Leftpos until a[Leftpos] >= pivot Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 59. 50 30 20 60 10 110 120 40 80 100 90 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Rightpos 110 > 70 and 40 < 70 (swap) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 60. Rightpos 50 30 20 60 10 40 120 110 80 100 90 70 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Eventually this stops when the two variables cross paths The Partition algorithm finishes by swapping the pivot at position last with the item at Leftpos Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 61. Rightpos 50 30 20 60 10 40 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 70 Pivot First Last Quick Sort Leftpos Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 62. Quicksort starts by partitioning the array. For the example above, the result of the first partition is 50 30 20 60 10 40 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] All are ≤ 70 All are > 70 The pivot value, 70, holds the same position it would if the array were sorted. All items to the left of it are smaller and all items to the right of it are larger. Quick Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 63. • The pivot value, 70, holds the same position it would if the array were sorted. • All items to the left of it are smaller and all items to the right of it are larger. • Thus, the 70 is in the right spot and need not be considered further. • Quicksort can continue by simply applying itself recursively to the two segments of the array above and below the pivot. Quick Sort Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 64. 50 30 20 60 10 40 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Quick Sort 10 30 20 40 50 60 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 10 20 30 40 50 60 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 65. Quick Sort 10 20 30 40 50 60 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 10 20 30 40 50 60 70 110 80 100 90 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 10 20 30 40 50 60 70 80 90 100 110 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 66. Quick Sort 10 20 30 40 50 60 70 80 90 100 110 120 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] Sorted Array Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 67. Quick Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane #𝒊𝒏𝒄𝒍𝒖𝒅𝒆 < 𝒔𝒕𝒅𝒊𝒐. 𝒉 > 𝒊𝒏𝒕 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅); 𝒗𝒐𝒊𝒅 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅); 𝒗𝒐𝒊𝒅 𝒎𝒂𝒊𝒏() { 𝒊𝒏𝒕 𝒊; 𝒊𝒏𝒕 𝒂𝒓𝒓[𝟏𝟎] = {𝟗𝟎, 𝟐𝟑, 𝟏𝟎𝟏, 𝟒𝟓, 𝟔𝟓, 𝟐𝟖, 𝟔𝟕, 𝟖𝟗, 𝟑𝟒, 𝟐𝟗}; 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂𝒓𝒓, 𝟎, 𝟗); 𝒑𝒓𝒊𝒏𝒕𝒇("𝒏 𝑻𝒉𝒆 𝒔𝒐𝒓𝒕𝒆𝒅 𝒂𝒓𝒓𝒂𝒚 𝒊𝒔: 𝒏"); 𝒇𝒐𝒓(𝒊 = 𝟎; 𝒊 < 𝟏𝟎; 𝒊 + +) 𝒑𝒓𝒊𝒏𝒕𝒇(" %𝒅𝒕", 𝒂𝒓𝒓[𝒊]); }
  • 68. Quick Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒊𝒏𝒕 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅) { 𝒊𝒏𝒕 𝒍𝒆𝒇𝒕, 𝒓𝒊𝒈𝒉𝒕, 𝒕𝒆𝒎𝒑, 𝒍𝒐𝒄, 𝒇𝒍𝒂𝒈; 𝒍𝒐𝒄 = 𝒍𝒆𝒇𝒕 = 𝒃𝒆𝒈; 𝒓𝒊𝒈𝒉𝒕 = 𝒆𝒏𝒅; 𝒇𝒍𝒂𝒈 = 𝟎; 𝒘𝒉𝒊𝒍𝒆(𝒇𝒍𝒂𝒈 ! = 𝟏) { 𝒘𝒉𝒊𝒍𝒆((𝒂[𝒍𝒐𝒄] <= 𝒂[𝒓𝒊𝒈𝒉𝒕]) && (𝒍𝒐𝒄! = 𝒓𝒊𝒈𝒉𝒕)) 𝒓𝒊𝒈𝒉𝒕 − −; 𝒊𝒇(𝒍𝒐𝒄 == 𝒓𝒊𝒈𝒉𝒕) 𝒇𝒍𝒂𝒈 = 𝟏; 𝒆𝒍𝒔𝒆 𝒊𝒇(𝒂[𝒍𝒐𝒄] > 𝒂[𝒓𝒊𝒈𝒉𝒕]) { 𝒕𝒆𝒎𝒑 = 𝒂[𝒍𝒐𝒄]; 𝒂[𝒍𝒐𝒄] = 𝒂[𝒓𝒊𝒈𝒉𝒕]; 𝒂[𝒓𝒊𝒈𝒉𝒕] = 𝒕𝒆𝒎𝒑; 𝒍𝒐𝒄 = 𝒓𝒊𝒈𝒉𝒕; }
  • 69. Quick Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒊𝒇(𝒇𝒍𝒂𝒈! = 𝟏) { 𝒘𝒉𝒊𝒍𝒆((𝒂[𝒍𝒐𝒄] >= 𝒂[𝒍𝒆𝒇𝒕]) && (𝒍𝒐𝒄! = 𝒍𝒆𝒇𝒕)) 𝒍𝒆𝒇𝒕 + +; 𝒊𝒇(𝒍𝒐𝒄 == 𝒍𝒆𝒇𝒕) 𝒇𝒍𝒂𝒈 = 𝟏; 𝒆𝒍𝒔𝒆 𝒊𝒇(𝒂[𝒍𝒐𝒄] < 𝒂[𝒍𝒆𝒇𝒕]) { 𝒕𝒆𝒎𝒑 = 𝒂[𝒍𝒐𝒄]; 𝒂[𝒍𝒐𝒄] = 𝒂[𝒍𝒆𝒇𝒕]; 𝒂[𝒍𝒆𝒇𝒕] = 𝒕𝒆𝒎𝒑; 𝒍𝒐𝒄 = 𝒍𝒆𝒇𝒕; } } } 𝒓𝒆𝒕𝒖𝒓𝒏 𝒍𝒐𝒄; }
  • 70. Quick Sort (Program) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒗𝒐𝒊𝒅 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂[], 𝒊𝒏𝒕 𝒃𝒆𝒈, 𝒊𝒏𝒕 𝒆𝒏𝒅) { 𝒊𝒏𝒕 𝒍𝒐𝒄; 𝒊𝒇(𝒃𝒆𝒈 < 𝒆𝒏𝒅) { 𝒍𝒐𝒄 = 𝒑𝒂𝒓𝒕𝒊𝒕𝒊𝒐𝒏(𝒂, 𝒃𝒆𝒈, 𝒆𝒏𝒅); 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂, 𝒃𝒆𝒈, 𝒍𝒐𝒄 − 𝟏); 𝒒𝒖𝒊𝒄𝒌𝑺𝒐𝒓𝒕(𝒂, 𝒍𝒐𝒄 + 𝟏, 𝒆𝒏𝒅); } }
  • 71. Radix Sort • Radix sort is one of the sorting algorithms used to sort a list of integer numbers in order. • In radix sort algorithm, a list of integer numbers will be sorted based on the digits of individual numbers. • Sorting is performed from least significant digit to the most significant digit. • Radix sort algorithm requires the number of passes which are equal to the number of digits present in the largest number among the list of numbers. • For example, if the largest number is a 3 digit number then that list is sorted with 3 passes. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 72. Radix Sort (Algorithm) The Radix sort algorithm is performed using the following steps... • Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9. • Step 2 - Consider the least significant digit of each number in the list which is to be sorted. • Step 3 - Insert each number into their respective queue based on the least significant digit. • Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues. • Step 5 - Repeat from step 3 based on the next least significant digit. • Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant digit. Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 73. Radix Sort (Algorithm) Example Consider the following list of unsorted integer numbers 82, 901, 100, 12, 150, 77, 55, 23 Step 1: Define 10 queues each represents a bucket for digit from 0 to 9 Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane queue 0 1 2 3 4 5 6 7 8 9
  • 74. Radix Sort (Algorithm) Step 2: Insert all the numbers of the list into respective queue based on the Least Significant Digit (once placed digit) of every number 82, 901, 100, 12, 150, 77, 55, 23 Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 150 100 901 12 82 23 55 77 queue 0 1 2 3 4 5 6 7 8 9 Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider the list for next step as input list 100, 150, 901, 82, 12, 23, 55, 77
  • 75. Radix Sort (Algorithm) Step 2: Insert all the numbers of the list into respective queue based on the next Least Significant Digit (tens placed digit) of every number 100, 150, 901, 82, 12, 23, 55, 77 Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 901 100 12 23 55 150 77 82 queue 0 1 2 3 4 5 6 7 8 9 Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider the list for next step as input list 100, 901, 12, 23, 150, 55, 77, 82
  • 76. Radix Sort (Algorithm) Step 2: Insert all the numbers of the list into respective queue based on the next Least Significant Digit (hundreds placed digit) of every number 100, 901, 12, 23, 150, 55, 77, 82 Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 82 77 55 23 12 150 100 901 queue 0 1 2 3 4 5 6 7 8 9 Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider the list for next step as input list 12, 23, 55, 77, 82, 100, 150, 901 List got sorted in the increasing order
  • 77. Radix Sort (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
  • 78. Radix Sort (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒊𝒇 = 𝒎𝒂𝒙 (((𝒂𝒓𝒓𝒂𝒚[𝒊] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎) > 𝒎𝒂𝒙){𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟏; 𝒊 < 𝒔𝒊𝒛𝒆; 𝒊 + +) 𝒊𝒏𝒕 𝒎𝒂𝒙 = (𝒂𝒓𝒓𝒂𝒚[𝟎] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎; 𝒊𝒏𝒕 𝒐𝒖𝒕𝒑𝒖𝒕[𝒔𝒊𝒛𝒆 + 𝟏]; {𝒗𝒐𝒊𝒅 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈𝑺𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆, 𝒊𝒏𝒕 𝒑𝒍𝒂𝒄𝒆) / / 𝑼𝒔𝒊𝒏𝒈 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈 𝒔𝒐𝒓𝒕 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒕𝒉𝒆 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒊𝒏 𝒕𝒉𝒆 𝒃𝒂𝒔𝒊𝒔 𝒐𝒇 𝒔𝒊𝒈𝒏𝒊𝒇𝒊𝒄𝒂𝒏𝒕 𝒑𝒍𝒂𝒄𝒆𝒔 𝒄𝒐𝒖𝒏𝒕[(𝒂𝒓𝒓𝒂𝒚[𝒊] / 𝒑𝒍𝒂𝒄𝒆) % 𝟏𝟎] + +; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟎; 𝒊 < 𝒔𝒊𝒛𝒆; 𝒊 + +)// 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆 𝒄𝒐𝒖𝒏𝒕 𝒐𝒇 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔𝒄𝒐𝒖𝒏𝒕[𝒊] = 𝟎; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟎; 𝒊 < 𝒎𝒂𝒙; + +𝒊)𝒊𝒏𝒕 𝒄𝒐𝒖𝒏𝒕[𝒎𝒂𝒙 + 𝟏]; }𝒂𝒓𝒓𝒂𝒚[𝒊];
  • 79. Radix Sort (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane 𝒄𝒐𝒖𝒏𝒕 𝒊 += 𝒄𝒐𝒖𝒏𝒕 𝒊 − 𝟏 ; 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟏; 𝒊 < 𝟏𝟎; 𝒊 + +)// 𝑪𝒂𝒍𝒄𝒖𝒍𝒂𝒕𝒆 𝒄𝒖𝒎𝒎𝒖𝒍𝒂𝒕𝒊𝒗𝒆 𝒄𝒐𝒖𝒏𝒕
  • 80. Radix Sort (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane }𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈𝑺𝒐𝒓𝒕(𝒂𝒓𝒓𝒂𝒚, 𝒔𝒊𝒛𝒆, 𝒑𝒍𝒂𝒄𝒆); 𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒑𝒍𝒂𝒄𝒆 = 𝟏; 𝒎𝒂𝒙 / 𝒑𝒍𝒂𝒄𝒆 > 𝟎; 𝒑𝒍𝒂𝒄𝒆 ∗ = 𝟏𝟎)// 𝑨𝒑𝒑𝒍𝒚 𝒄𝒐𝒖𝒏𝒕𝒊𝒏𝒈 𝒔𝒐𝒓𝒕 𝒕𝒐 𝒔𝒐𝒓𝒕 𝒆𝒍𝒆𝒎𝒆𝒏𝒕𝒔 𝒃𝒂𝒔𝒆𝒅 𝒐𝒏 𝒑𝒍𝒂𝒄𝒆 𝒗𝒂𝒍𝒖𝒆. 𝒊𝒏𝒕 𝒎𝒂𝒙 = 𝒈𝒆𝒕𝑴𝒂𝒙(𝒂𝒓𝒓𝒂𝒚, 𝒔𝒊𝒛𝒆);// 𝑮𝒆𝒕 𝒎𝒂𝒙𝒊𝒎𝒖𝒎 𝒆𝒍𝒆𝒎𝒆𝒏𝒕{𝒗𝒐𝒊𝒅 𝒓𝒂𝒅𝒊𝒙𝒔𝒐𝒓𝒕(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆) / / 𝑴𝒂𝒊𝒏 𝒇𝒖𝒏𝒄𝒕𝒊𝒐𝒏 𝒕𝒐 𝒊𝒎𝒑𝒍𝒆𝒎𝒆𝒏𝒕 𝒓𝒂𝒅𝒊𝒙 𝒔𝒐𝒓𝒕
  • 81. Radix Sort (Algorithm) Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane }𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒂𝒓𝒓𝒂𝒚, 𝒏); 𝒓𝒂𝒅𝒊𝒙𝒔𝒐𝒓𝒕(𝒂𝒓𝒓𝒂𝒚, 𝒏); 𝒊𝒏𝒕 𝒏 = 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓𝒂𝒚) / 𝒔𝒊𝒛𝒆𝒐𝒇(𝒂𝒓𝒓𝒂𝒚[𝟎]); 𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[] = {𝟏𝟐𝟏, 𝟒𝟑𝟐, 𝟓𝟔𝟒, 𝟐𝟑, 𝟏, 𝟒𝟓, 𝟕𝟖𝟖}; {𝒊𝒏𝒕 𝒎𝒂𝒊𝒏() // 𝑫𝒓𝒊𝒗𝒆𝒓 𝒄𝒐𝒅𝒆}𝒑𝒓𝒊𝒏𝒕𝒇("𝒏"); }𝒑𝒓𝒊𝒏𝒕𝒇("%𝒅 ", 𝒂𝒓𝒓𝒂𝒚[𝒊]); {𝒇𝒐𝒓 (𝒊𝒏𝒕 𝒊 = 𝟎; 𝒊 < 𝒔𝒊𝒛𝒆; + +𝒊) {𝒗𝒐𝒊𝒅 𝒑𝒓𝒊𝒏𝒕𝑨𝒓𝒓𝒂𝒚(𝒊𝒏𝒕 𝒂𝒓𝒓𝒂𝒚[], 𝒊𝒏𝒕 𝒔𝒊𝒛𝒆) // 𝑷𝒓𝒊𝒏𝒕 𝒂𝒏 𝒂𝒓𝒓𝒂𝒚