Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
22 views
28 pages
Chapter 2
it is helpful
Uploaded by
pranitadahiwadkar3
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save chapter-2 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
22 views
28 pages
Chapter 2
it is helpful
Uploaded by
pranitadahiwadkar3
AI-enhanced title
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save chapter-2 For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save chapter-2 For Later
You are on page 1
/ 28
Search
Fullscreen
SVERI’s College of Engineering(Poly.) Pandharpur © Name of Staff:- Prof. Bhandare P. S. © Name of Subject:- Data structure Using ‘C’ © Subject code:- 17330 / 22317 ® Class:- SYCO © Department:- Computer Engineering CHAPTER 2 SEARCHING & SORTING -by- Prof. Bhandare P. S. SVERI’s COE(Poly), Pandharpur Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 1Syllabus: 2.1 Searching » Linear Search > Binary Search 2.2 Sorting > Bubble > Selection > Insertion > Quick > Radix > Merge > Shell sort Data structure Using *C” SVERI’s College of Engineering(Poly.) Pandharpur (Mr. Bhandare PS.) Chapter — Searching & Sorting 2 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Assignment No. 2 Define searching. Give its type. (2M) Write Linear search algorithm. (2M) Describe binary search algorithm. Give example to search an element using binary search. (4M) 4, Sort following elements by Radix sort algorithm : 87, 3, 234, 729, 359, 45, 8,379, 320,422 (4M) Write an algorithm for insertion sort and arrange given numbers in ascending order using insertion sort : 9, 15, 5, 20, 10 (8M) 6. Sort the following numbers in ascending order using quick sort. Given numbers 50, 2, 6, 22, 3, 39, 49, 25, 18,5 [4m] Differentiate between binary search and sequential search (linear) [4m] Sort the following numbers in ascending order using Bubble sort. Given numbers: 29, 35, 3, 8, 11, 15, 56, 12, 1, 4, 85, 5 & write the output after each interaction, [6m] 9. Describe working of bubble sort with example. (4M) ~~ Programs: 1, Write program to search an element in an array. Display position of element. (4M) . Write C program for performing following operations on array : insertion, display. [4m] 3. Write a program for Bubble sort 4. WAP for linear search to fine position of element. 5. WAP for binary search to fine position of element. 6. Write a program for insertion sort. 7 8. . Write a program for Selection sort. Write C program for performing following operations on array : deletion, display. [4m] Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 3 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur About title of chapter. © In this chapter we are going to study searching & sorting technique. © This chapter gives introduction about data structure operations. About central idea of chapter: © Central Idea of this chapter is we can understand what is searching & sorting used in data structure... © Also different types of searching & sorting on Data Structure About Importance of chapter: * To study of chapter is important because it will gives basic concepts searching element in data structure. © Also this chapter gives idea about operation for sorting performed on data structure Objectives : By studying this chapter student will understand. © What is searching. © Different types of searching © What is sorting. © Different types of sorting. * Algorithms & programs for searching & sorting Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 4 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur SEARCHING—AN INTRODUCTION We often spend time in searching for the desired item, If the data is kept properly in sorted order, then searching becomes very easy and efficient Searching is an operation which finds the place of a given element in the list. The search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or not. Some of the standard searching methods are discussed below. 1. Linear search 2. Binary Search 3. Hashing Linear or Sequential Search: ‘* This is the simplest method for searching. In this method the element to be found is searched sequentially in the list. * This method can be used on a sorted or an unsorted list © In case of a sorted list searching starts from 0" element and continues until the element is found or the element whose value is greater than (assuming the list is sorted in ascending order) the value being searched is reached. © As against this, searching in case of unsorted list starts from the 0" element and continues until the element is found or the end of list is reached. For example. 10 1 9 i 46 20 15 0 2 7) Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 5 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur © The list given above is the list of elements in an unsorted array. The array contains 10 elements. ‘* Suppose the element to be searched is 46 * So 46 is compared with all the elements starting from the Oth clement and searching process ends where 46 is found or the list ends. * The performance of the linear search can be measured by counting the comparisons done to find out an element. © The number of comparisons is 0(n). Algorithm for Linear search: Step I: Set ito 1 Step 2: if i> n then go to step 7 Step 3: if Afi] =x then go to step 6 Step 4: Set itoi +1 Step 5: Go to Step 2 Step 7: Print element not found Step 8: Exit eo eee eee Program for linear search: Hinclude
void main(){ int af 10],i.n,m,c=0; printf{"Enter the size of an array: scanft"%d",&n): printf("Enter the elements of the array: "); for(i-0;i<—n-13i++){ seanft"%d" afi): 3 printf{("Enter the number to. be search: "); scanft"%d" 8m): Data structure Using *C’- (Mr. Bhandare PS.) Step 6: Print Element x Found at index i and go to step 8 for(i=0:i
x . set upperBound = midPoint - 1 . if AfmidPoint] = x . EXIT: x found at location midPoint © end while * end procedure Program: 1. #include
6. printf("Enter the size of an 2. #include
array: "); 3. void main() { 7. scanft"%d" &n); 4, int a[100].i,n,ele.c=0,m,Lh; 8. IF1; 5. clrser(): 9, h=n; Data structure Using*C’- (Mr. Bhandare PS.) Chapter ~ Searching & Sorting 8 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur 10.printf("Enter_ the elements of 23.h=m; the array: "); 24.) 11. for(i=O;i<—n-1:i+4) { 25.else { 12.scanf("%d" ali); 26.|=m*1; 13.} 273 14.printf("Enter the number to be 28.) search: "); 29,if(e==0) 15.scanf{"%d" ele): 16.while(I<=h) { a, printf("The number is not in the list"); 17.m=(l+hy/2; 30.else 18.iffele—a[m]) { a. printf("The number is 19. found"); 20.break; 31. getch(); 21} 32.3 22.clse iffele
list{i+1] . swap(listfi). list{i+1]) ° endif, © end for . return list * end BubbleSort Program for Bubble sort: 4include
#inelude
void main() { int s.temp,i,j.a[20]; elrser(); printi("Enter total numbers of elements: scanf{""%d",&s); printf("Enter %d elements: " for(i=0;i
ali+ 1) j temp=alj]; aliJ=ali+ 1]: alj+1]-temp; } i } printf(" After sorting: "); forti=0;i
[2] [5] 7 Unsorted Array 14 © scone wih ote scarSVERI’s College of Engineering(Poly.) Pandharpur 12. if list{j]
void selection_sort(); . int a[30], n; |. void main() { inti; printf("\nEnter size of an array: "); scanf("%d", &n); printf("\nEnter of an array:in"); 9. for(i=0; i
As Pod therfore 7 will be moved forward and 4 will be moved to 7's position STEP 2. As 725,7 will be moved forward but 4 <5,s0 no change in positon of 4 ‘nd 5 will be moved to position of 7 STEP 3. As all the element on left side of 2 are greater than 2,50 all the elements will be moved ff here checking forward and 2 will be shifted on eft side of to positon of & 2 STEP 4. Data structure Using *C’- (Mr. Bhandare PS.) Chapter ~ Searching & Sorting 16 © scanned wt onEN SenneAlgorithm for insertion sort . Step 2 — Pick next element Bene the value to be sorted Step 5 — Insert the value aw Program for Insertion sort: #include
#include
void main() 4 int ij.s.temp.a[20]; clrser(); printf("Enter total elements: "); scanf{"%d" &s); printf("Enter %d elements: ".s); for(i=0si
=0)) t alj+1]=alj]: Fb } alj+l]=temp; } printf("Afer sorting: "); forli=O;i
pivot then stop. Step 4 - Decrement j until list{j]
. Step 7 - Exchange the pivot element with list[j] element. Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 20 © scanned wt onEN SenneSVERI's College of Engineering(Poly.) Pandharpur Eg (le J 2° [o> T+ [7 To [a2 [= | 22 ] wifcomverse'on tot point leftmark ——> + right mark 26<54 move to ight (= Teo Tee TT To [os [oe Lo) tris leftmark Fightmark [= Teo Too TT [2 | | ss [20] “asst” leftmark Fightmark Boome tottmark Fightmark how continue maving lonmane and Hanuman Le: [ee Leo | [x To: [ee Tos Toe) est sce exchange 77 ha a4 ——— awe Tether rightnarte 7784 stop (lesN] 2 [> [ee [os Pea] [ss Lee) nina vignenaiecittart Split point found exchange 54 and 31 rightmark _lertmark unit they cross. OTe EI Te Js < >! [> J» Jed) [7 |=] >] quicksort left half quicksort right half Data structure Using *C’- (Mr. Bhandare PS.) Chapter — Searching & Sorting 21 © scanned tone ScanSVERI’s College of Engineering(Poly.) Pandharpur Eg. Unsorted Array FE ed PE bd 9 ed EW cad Fs [se [s4 [43 [+1 [15 [20 [20 Tas [27 [a2 | ——<—— eS? Right Lon a one eee [se[S= [3] 70 [as [20 [25 [ss [27 ]52] 25-55 [=7 [os [=3 Ts [ss [20 [#6 1-2 [20 [a2] 23288 ae ae [2z[ 52) e 9 [es eo) 2s [es [55 1S) Sa > ss [27 [26 [22 [9 [os [0 [> [5 [20 [2] 285 82 Move to Right _ [=z [2s 20 [28 [25] <9 [5+ [2s [se 52] 53> 55 Fig. Finding Pivot Value in an Array Worst Case : O(n?) Best Case : O (n log n) Average Case : O(n log n) Data structure Using *C- (Mr. Bhandare PS.) Chapter ~ Searching & Sorting 22 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Merge Sort: © Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves ‘© Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. © Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list Idea: © Divide the unsorted list into N sublists, each containing | element. © Take adjacent pairs of two singleton lists and merge them to form a list of 2 elements. N will now convert into N/2 lists of size 2 © Repeat the process till a single sorted list of obtained. ‘© While comparing two sublists for merging, the first element of both lists is taken into consideration, While sorting in ascending order, the element that is of a lesser value becomes a new element of the sorted list. This procedure is repeated until both the smaller sublists are empty and the new combined sublist comprises all the elements of both the sublists. Complexity of Merge sort: ‘© Worst Case Time Complexity: O(n*log n) © Best Case Time Complexity: O(n*log n) © Average Time Complexity: O(n*log n) Algorithm: © MergeSort(A, p,r) © mergeSort(A, p,q) * lfper © mergeSort(A, q+ 1.1) . return; © merge(A, p. q,1) 2 q=(ptry/2; Data structure Using *C’- (Mr. Bhandare PS.) Chapter — Searching & Sorting 23 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Bg. These numbers indicate | 38 |27 | 43/3 | 9 | 82 | 10 Tee 38|27]43|3 9} 82|10 2 12 ¥. 38 | 27 43/3 9|82 10 3 7 13 VW pan a 38 [ 2 43 3 9 82 10 Te A || Selle ne Ig ¥ 27|38 3| a3 9 | 82 10 12 ve 18 ir y 3|27| 38 | 43 9} 10 | 82 19 3] 9|10|27]|38| 43 |82|20 Shell Sort: * As we know, insertion and selection sort behave differently. Selection sort moves the entries very efficiently but does not make redundant comparisons. * In its best case, insertion sort does the minimum number of comparisons but is inefficient in moving entries only one place at a time, as it compares only adjacent keys. « Ifwe were to modify it, first comparing the keys far apart, then it could sort the entries. Afterwards the entries closer together would be sorted and finally the increment between the keys would be reduced to 1 to ensure that the list is completely in order. © This is the idea implemented in 1959 by Donald. L. Shell in the surfing method bearing his name. © The method is also called diminishing-increment sort. Data structure Using *C*- (Mr. Bhandare PS.) Chapter ~ Searching & Sorting 24 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur © The shell sort can be explained with the following example. ‘© Suppose we want to sort: 9, 1, 8,4, 6, 3,5, 7, 11, 14, 10, 12, 0,2, 8 © The given numbers are first sorted at distance 5 from each other and then resorted with distance 3 and finally insertion sort has been performed: eg Iteration 1: 9. “9 1 8. 4. | |1,|| 8 4 |e 6. 305 7 UU i 3 10 12 0 2 8 5. Applying insertion sort in above list T- 1 3 1 0 2 6 1 Qs 7 4 8 1 lo 12 8 Wl 1 Collect element from Left to right & top to bottom 0. 2 8- Iteration 2: s 1 0- Collect list 2 3 ih 0 6- 2 6 2 9. 5 7 4 3 8 10 12 1. 8 ou 4. Apply insertion sort in above Lists 8. 2 1 0 I 3 6 4 1 5 a > 8- 8 10 12 1 8 i 14 14- “14 Collect element from Left to right & top to bottom Data structure Using*C’- (Mr. Bhandare PS.) Chapter ~ Searching & Sorting 5 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Iteration 3: 2,1,0,3,6.4,5,7,9,8,10,12,8,11,14 Apply insertion sort: Itis not mandatory to make $,3and 1 as increments. Many other choices are also be made but it should be considered that the choices like power of 2 like 8.4.2.1 Algorithm: 1. In the first iteration, the elements are splitted with a gap of say. 5. 3, ete. and are sublisted 2. The sublist obtained after splitting is again sorted. 3. The sorted sublist is recombined. 4. The sorted list, in the second iteration, is again sublisted with a gap (here, itis 3), 5. Repeat through steps 2 and 3. Complexity of Shell sort: © Worst Case Time Complexity: O(n*log n?) * Best Case Time Complexity : O(n'log n) © Average Time Complexity : O(n"log n°) Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 26 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Summer- 19 (17330) . Define searching. Give its type. (2M) . Write Linear search algorithm. (2M) |. Describe binary search algorithm. Give example to search an element |. using binary search, (4M) . Sort following elements by Radix sort algorithm 87, 3, 234, 729, 359, 45, 8, 379, 320,422 (4M) 6. Write program to search an element in an array. Display position of element. (4M) . Write an algorithm for insertion sort and arrange given numbers in ascending order using insertion sort 9, 15, 5, 20, 10 (8M) wrens [ Summer- 19 (22317) . Explain the working of Binary search with an example.[4m] Write C program for performing following operations on array : insertion, display. [4m] Sort the following numbers in ascending order using quick sort. Given numbers 50, 2, 6, 22, 3, 39, 49, 25, 18,5 [4m] 4. Differentiate between binary search and sequential search (linear) [4m] Sort the following numbers in ascending order using Bubble sort. Given numbers: 29, 35, 3, 8, 11, 15, 56, 12, 1, 4, 85, 5 & write the output after each interaction. [6m] Winter- 18 (22317) . Desoribe working of linear search with example. (4M) 2. Write ‘c’ program for deletion of an element from an array. (4M) 3. Find the position of element 29 using binary search method in an array “A given below. Show each step. A={11,5, 21, 3, 29, 17, 2, 43} (4M) 4. Describe working of bubble sort with example. (4M) 5. Describe working of selection sort method. Also sort given input list in ascending order using selection sort input list — 55, 25, 5, 15, 35. (6M) Winter-18 (17330) Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 27 © scanned wt onEN SenneSVERI’s College of Engineering(Poly.) Pandharpur Define sorting. Write its types. (2M) . Write a ‘program inc’ language for selection sort.(4M) Describe binary search with example. (4M) . Perform bubble sort on following data to sortall elements in ascending order. 15,10,02,35,08 (4M) Consider the following array : 4M) 55, 65,25.75,45,85,10 Write stepwise procedure to find 45 using linear search. 6. Describe working of Radix sort with example. (4M) -wNe Summer- 18 State any two differences between linear search and binary search. (2M) . Give complexity of following methods : i) Bubble sort ii) Radix sort iii) Linear search iv) Binary search. (2M) Perform radix sort on the following list to arrange all array elements in ascending order : 18, 253, 1000, 2, 80, 75, 58 (4M) 4, Describe working of selection sort method with suitable example. (4M) Write a °C” program to perform bubble sort on array of size N. (4M) 6. Describe working of binary search method. Give stepwise procedure to search 65 in the following list : List : 23, 12, 5,29, 10, 65, 55, 70 (8M) Summer- 17 Define searching and enlist its types.[2m] Write a program for Binary search [4m] Write a program for selection sort. [4m] . Elaborate the steps for performing insertion sort for given elements of array. 30 10.40 50.2045 [4m] 5. Write a program for linear search, Find position of element 30 using linear search algorithm in given sequence. 10 5 20 25 8 30 40 [8m] Rene Data structure Using *C”- (Mr. Bhandare P.S.) Chapter ~ Searching & Sorting 28 © scanned wt onEN Senne
You might also like
Unit 4 1048 1698311627 RPWPFV
PDF
No ratings yet
Unit 4 1048 1698311627 RPWPFV
100 pages
Data Structures Through C R20 - Unit-5
PDF
No ratings yet
Data Structures Through C R20 - Unit-5
50 pages
UNIT-5-ch-2 (SEARCH& SORT)
PDF
No ratings yet
UNIT-5-ch-2 (SEARCH& SORT)
29 pages
CH2 Searching and Sorting Notes
PDF
No ratings yet
CH2 Searching and Sorting Notes
16 pages
C Unit-5
PDF
No ratings yet
C Unit-5
41 pages
Module 5 Basic Algorithm
PDF
No ratings yet
Module 5 Basic Algorithm
11 pages
Dsu Microproject Final1
PDF
No ratings yet
Dsu Microproject Final1
24 pages
Searching
PDF
No ratings yet
Searching
16 pages
DS - Unit 3
PDF
No ratings yet
DS - Unit 3
29 pages
Binary Search Algorithm - Data Structure
PDF
No ratings yet
Binary Search Algorithm - Data Structure
3 pages
DS Unit-2 SearchSort
PDF
No ratings yet
DS Unit-2 SearchSort
24 pages
Unit 3
PDF
No ratings yet
Unit 3
32 pages
Binary Search
PDF
No ratings yet
Binary Search
10 pages
Binary Search
PDF
No ratings yet
Binary Search
5 pages
DS Unit 5
PDF
No ratings yet
DS Unit 5
27 pages
Searching and Sorting
PDF
No ratings yet
Searching and Sorting
14 pages
Unit - 2
PDF
No ratings yet
Unit - 2
59 pages
Final Term Course (Week No. 07 - 16)
PDF
No ratings yet
Final Term Course (Week No. 07 - 16)
50 pages
1 Module 5
PDF
No ratings yet
1 Module 5
65 pages
Search Sort 7 Feb 17
PDF
No ratings yet
Search Sort 7 Feb 17
33 pages
Module-1 (Searching & Sorting)
PDF
No ratings yet
Module-1 (Searching & Sorting)
27 pages
Dsu MP
PDF
No ratings yet
Dsu MP
25 pages
Unit 6 Dsa
PDF
No ratings yet
Unit 6 Dsa
152 pages
Ds V UNIT
PDF
No ratings yet
Ds V UNIT
17 pages
2 Searching and Sorting
PDF
No ratings yet
2 Searching and Sorting
41 pages
Unit 3 Ds
PDF
No ratings yet
Unit 3 Ds
35 pages
Unit - 4 Searching
PDF
No ratings yet
Unit - 4 Searching
38 pages
Chapter 2
PDF
No ratings yet
Chapter 2
9 pages
DSA Chapter 2
PDF
No ratings yet
DSA Chapter 2
57 pages
Searching and Sorting
PDF
No ratings yet
Searching and Sorting
27 pages
Unit V - Searching and Sorting
PDF
No ratings yet
Unit V - Searching and Sorting
24 pages
CS 253 4 Sorting
PDF
No ratings yet
CS 253 4 Sorting
111 pages
Searching Algorithms: Harshita Sharma Delhi Technological University Department of CSE
PDF
No ratings yet
Searching Algorithms: Harshita Sharma Delhi Technological University Department of CSE
32 pages
Insertion Sort and Searching: Dept. of Computer Science Faculty of Science and Technology
PDF
No ratings yet
Insertion Sort and Searching: Dept. of Computer Science Faculty of Science and Technology
13 pages
12 Computer Science Notes CH05 Data Structures
PDF
No ratings yet
12 Computer Science Notes CH05 Data Structures
28 pages
10 - 19UCSES301 - A - 7 - 29unit V Module
PDF
No ratings yet
10 - 19UCSES301 - A - 7 - 29unit V Module
23 pages
Unit-IV Notes
PDF
No ratings yet
Unit-IV Notes
30 pages
1 Unit 3 Searching and Hashing
PDF
No ratings yet
1 Unit 3 Searching and Hashing
84 pages
Lecture 05.2
PDF
No ratings yet
Lecture 05.2
13 pages
Arrays: Searching and Sorting
PDF
No ratings yet
Arrays: Searching and Sorting
32 pages
2 and 3
PDF
No ratings yet
2 and 3
33 pages
Week 7
PDF
No ratings yet
Week 7
72 pages
DSA - Searching PDF
PDF
100% (1)
DSA - Searching PDF
50 pages
Module 6
PDF
No ratings yet
Module 6
47 pages
Unit V
PDF
No ratings yet
Unit V
94 pages
Introduction To Searching in Data Structures
PDF
No ratings yet
Introduction To Searching in Data Structures
6 pages
DS Unit 5
PDF
No ratings yet
DS Unit 5
50 pages
DS Unit 1
PDF
No ratings yet
DS Unit 1
45 pages
DS UNIT 5 Optimize
PDF
No ratings yet
DS UNIT 5 Optimize
32 pages
CMP 202 (Searching)
PDF
No ratings yet
CMP 202 (Searching)
26 pages
Data Structure B Tech 2 Year Unit 3 Notes
PDF
No ratings yet
Data Structure B Tech 2 Year Unit 3 Notes
43 pages
Unit II
PDF
No ratings yet
Unit II
150 pages
Binary, Merge, Quick
PDF
No ratings yet
Binary, Merge, Quick
27 pages
Best Sorting Notes
PDF
No ratings yet
Best Sorting Notes
19 pages
Searching Techniques
PDF
No ratings yet
Searching Techniques
17 pages
Unit V
PDF
No ratings yet
Unit V
31 pages
DSU Microproject
PDF
No ratings yet
DSU Microproject
15 pages