3 DSA Searching and Sorting
3 DSA Searching and Sorting
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
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
1 2 4 5 6
3
7 12 5
7 42 101
35
Idea: Bubbling Up the Largest Element
Traverse a collection of elements
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
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
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
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
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
1 2 3 4 5 6
42 35 12 77 5 101
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?
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.”
{ 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
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
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