0% found this document useful (0 votes)
13 views40 pages

3 DSA Searching and Sorting

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views40 pages

3 DSA Searching and Sorting

Uploaded by

seharamjadnuml
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

DATA STRUCTURES

Searching Algorithms
Array Operations
 Insertion
 Operation of adding another element to an array
 How many steps in terms of n (number of elements in array)?
 At the end
 In the middle
 In the beginning
 n steps at maximum (move items to insert at given location)
 Deletion
 Operation of removing one of the elements from an array
 How many steps in terms of n (number of elements in array)?
 At the end
 In the middle
 In the beginning
 n steps at maximum (move items back to take place of deleted item)
Array Operations: Search Algorithms
 Operation of locating a specific data item in an array
 Successful: If location of the searched data is found
 Unsuccessful: Otherwise

 Complexity (or efficiency) of a search algorithm


 Number of comparisons f(n) required to locate data within array
 n is the number of elements within array

 Two algorithms for searching in arrays


 Linear search (or sequential search)
 Binary search
Linear Search
 Very spontaneous and simple algorithm

 Algorithm works as follows:

 Starts from the first element of the array

 Uses a loop to sequentially step through an array

 Compares each element with the data item being searched

 Stops when data item is found, or end of array is reached


Linear Search Algorithm
Calling Function searchList()
Analysis
 Advantage of linear search is its simplicity
 Easy to understand
 Easy to implement
 Does not require array to be in order (i.e., sorted)

 Disadvantage is its efficiency (or complexity)


 Worst case complexity: f ( n ) = n+1
 Number of steps are proportional to number n of elements in an
array

 If there are 20,000 items in an array


 Searched data item is stored in the 19,999th element
 Entire array has to be searched
Binary Search
 Binary search is more efficient than linear search
 Requires array to be in sorted order (i.e., ascending order)

 Algorithm works as follows:


 Starts searching from the middle element of an array

 If value of data item is less than the value of middle element


 Algorithm starts over searching the first half of the array

 If value of data item is greater than the value of middle element


 Algorithm starts over searching the second half of the array

 Algorithm continues splitting the array until data item is found


Binary Search Algorithm
Calling Function binarySearch()
Binary Search Example
DATA STRUCTURES

Sorting Algorithms
Sorting
 Sorting is a process in which records are arranged in ascending or descending
order

1 2 3 4 5 6

77 42 35 12 101 5

1 2 3 4 5 6

5 12 35 42 77 101
Sorting – Example Applications
 To prepare a list of student ID, names, and scores in a table (sorted
by ID or name) for easy checking
 To prepare a list of scores before letter grade assignment
 To produce a list of horses after a race (sorted by the finishing
times) for payoff calculation
 To prepare an originally unsorted array for ordered binary
searching
Sorting Algorithms
 Bubble sort

 Selection sort

 Insertion sort
Bubble sort
 Compares adjacent items and exchanges them if they are
out of order.
 Comprises of several passes.
 In one pass, the largest value has been “bubbled” to its
proper position.
 In second pass, the last value does not need to be
compared
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 4 5 6
3
7 12 5
7 42 101
35
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 3 4 5 6
77S ap
42 3 12 5
w
77 42 5 101
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 3 4 5 6
77 S ap 12 5
42 w35 3577 101
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 3 5 6
4 77S ap1 10
4 5
w 2 1
2 35 12
77
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 4 5 6
3
4 77 5
2 35 101
12 No need to
swap
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 4 5 6
3 ap
4 77 5 S 101
101
35 5
2
w
12
Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping

1 2 3 4 5 6
42 35 12 77 5 101

Largest value placed at end


Idea: Bubbling Up the Largest Element
 Traverse a collection of elements

 Move from the front to the end

 “Bubble” the largest value to the end using the operations


 Pair-wise comparison
 Swapping
1 2 3 4 5 6
42 35 12 77 5 10
1

Largest value correctly placed


How Many Times to Repeat “Bubble Up”
 Each time we bubble an element, we place it in its
correct location

 If we have n elements…
 Then we repeat the “bubble up” process n – 1 times
 This guarantees all n elements are correctly placed
“Bubbling” All the Elements
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
N-1

12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101

1 2 3 4 5 6
5 12 35 42 77 101
Reducing the Number of Comparisons
1 2 3 4 5 6
77 42 35 12 101 5
1 2 3 4 5 6
42 35 12 77 5 101

1 2 3 4 5 6
35 12 42 5 77 101

1 2 3 4 5 6
12 35 5 42 77 101

1 2 3 4 5 6
12 5 35 42 77 101
Already Sorted Collections?
 What if the collection was already sorted?
 What if only a few elements were out of place and after a couple of “bubble
ups,” the collection was sorted?

 We want to be able to detect this and “stop early”!

1 2 3 4 5 6
5 12 35 42 77 101
Using a Boolean “Flag”
 We can use a boolean variable to determine if any swapping
occurred during the “bubble up.”

 If no swapping occurred, then we know that the collection is


already sorted!

 This boolean “flag” needs to be reset after each “bubble up.”


Bubble Sort Algorithm
void bubbleSort (int S[ ], int length) {
{ int temp =
bool isSorted = false; S[i];

while(!isSorted) S[i] = S[i+1];

{ S[i+1] =
temp;
isSorted = true;
isSorted =
for(int i = 0; i<length; i+ false;
+)
}
{
}
if(S[i] > S[i+1])
length--;
}
Selection Sort
 Define the entire array as the unsorted portion of the
array

 While the unsorted portion of the array has more than


one element:
 Find its largest element
 Swap with last element (assuming their values are different)
 Reduce the size of the unsorted portion of the array by 1.
Selection Sort - Example
Original Array 14 2 10 5 1 3 17 7

Pass 1 14 2 10 5 1 3 7 17

Pass 2 7 2 10 5 1 3 14 17

Pass 3 7 2 3 5 1 10 14 17

Pass 4 1 2 3 5 7 10 14 17
Selection Sort Algorithm
Selection Sort vs. Bubble Sort
 The bubble sort is inefficient for large arrays
 Items only move by one element at a time

 The selection sort moves items immediately to their


final position
 Makes fewer exchanges
Time Complexity of Selection Sort
 The total number of comparisons is :-
 (N-1)+(N-2)+(N-3)+………… +1= N(N-1)/2
 We can ignore the constant 1/2
 We can express N(N-1) as N2 – N
 We can ignore N as well since N2 grows more rapidly than N,
making our algorithm O(N2)
Insertion Sort
 In insertion sort, each successive element in the array to be sorted is
inserted into its proper place with respect to the other, already sorted
elements.

 We divide our array in a sorted and an unsorted array

 Initially the sorted portion contains only one element: the first element
in the array.

 We take the second element in the array, and put it into its correct place

 A list of n elements will take at most n-1 passes to sort the


data
Insertion Sort – Example
Take array A[]=[7,4,5,2].
Insertion Sort Algorithm

You might also like