DS2
DS2
Linear Search
▪ Linear search is a very simple search algorithm. In this type of search, a sequential search is made
over all items one by one.
▪ Every item is checked and if a match is found then that particular item is returned, otherwise the
search continues till the end of the data collection.
▪ Algorithm
Linear Search ( Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
▪ Example
Object Oriented
GAWANDE’S INSTITUTE, GADGE NAGAR, AMRAVATI
Mobile No: 9096617225
Programming Using C++
Binary Search
▪ Binary search looks for a particular item by comparing the middle most item of the collection.
▪ If a match occurs, then the index of item is returned.
▪ If the middle item is greater than the item, then the item is searched in the sub-array to the left of
the middle item.
▪ Otherwise, the item is searched for in the sub-array to the right of the middle item.
▪ This process continues on the sub-array as well until the size of the subarray reduces to zero.
▪ For a binary search to work, it is mandatory for the target array to be sorted.
▪ Algorithm
Binary Search ( Data, LB, UB, Item, Loc)
Step 1: Set Start=LB, End=UB, Middle=INT(LB+UB)/2;
Step 2: Repeat Steps 3 & 4 while Start<=End and Data[Middle]!=Item
Step 3: If Item<Data[Middle] then set End=Middle-1
Else: set Start=Middle+1
Step 4: Set Middle=Int(Start+End/2)
Step 5: if Data[Middle]=Item then set Pos:= Middle else: set Pos:=Null
Step 6: Exit
▪ Example
Object Oriented
GAWANDE’S INSTITUTE, GADGE NAGAR, AMRAVATI
Mobile No: 9096617225
Programming Using C++
Bubble Sort
▪ Bubble sort is a simple sorting algorithm.
▪ This sorting algorithm is comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.
▪ Bubble sort works on the repeatedly swapping of adjacent elements until they are not in the
intended order.
▪ It is called bubble sort because the movement of array elements is just like the movement of air
bubbles in the water.
▪ Bubbles in water rise up to the surface; similarly, the array elements in bubble sort move to the
end in each iteration.
▪ Algorithm
Selection Sort
▪ Selection sort is a simple sorting algorithm.
▪ This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into
two parts, the sorted part at the left end and the unsorted part at the right end.
▪ Initially, the sorted part is empty and the unsorted part is the entire list.
▪ The smallest element is selected from the unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array.
▪ This process continues moving unsorted array boundary by one element to the right.
▪ Algorithm
Object Oriented
GAWANDE’S INSTITUTE, GADGE NAGAR, AMRAVATI
Mobile No: 9096617225
Programming Using C++
Insertion Sort
▪ Insertion sort works similar to the sorting of playing cards in hands.
▪ It is assumed that the first card is already sorted in the card game, and then we select an unsorted
card.
▪ If the selected unsorted card is greater than the first card, it will be placed at the right side;
otherwise, it will be placed at the left side.
▪ Similarly, all unsorted cards are taken and put in their exact place.
▪ Algorithm
Quick Sort
▪ Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into
smaller arrays.
▪ A large array is partitioned into two arrays one of which holds values smaller than the specified
value, say pivot, based on which the partition is made and another array holds values greater than
the pivot value.
▪ Algorithm
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
Radix Sort
▪ Radix sort is a step-wise sorting algorithm that starts the sorting from the least significant digit of
the input elements.
▪ Like Counting Sort and Bucket Sort, Radix sort also assumes something about the input elements,
that they are all k-digit numbers.
▪ The sorting starts with the least significant digit of each element.
▪ These least significant digits are all considered individual elements and sorted first; followed by
the second least significant digits.
▪ This process is continued until all the digits of the input elements are sorted.
▪ Algorithm
radixSort(arr)
1. max = largest element in the given array
2. d = number of digits in the largest element (or, max)
3. Now, create d buckets of size 0 - 9
4. for i -> 0 to d
sort the array elements using counting sort (or any stable sort) according to the digits at
the ith place