Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views111 pages

Sorting Searching

The lecture covers sorting and searching algorithms, emphasizing their importance in computer science. It discusses linear and binary search methods, along with various sorting algorithms like bubble sort and selection sort, highlighting their time complexities. The document includes code snippets for bubble sort and selection sort implementations.

Uploaded by

fk.fardin4585
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)
5 views111 pages

Sorting Searching

The lecture covers sorting and searching algorithms, emphasizing their importance in computer science. It discusses linear and binary search methods, along with various sorting algorithms like bubble sort and selection sort, highlighting their time complexities. The document includes code snippets for bubble sort and selection sort implementations.

Uploaded by

fk.fardin4585
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/ 111

Lecture 18

Sorting
CSE225: Data Structures and Algorithms
Sorting and Searching
• Fundamental problems in computer science and
programming
• Sorting done to make searching easier
• Multiple different algorithms to solve the same
problem
• How do we know which algorithm is "better"?
• Let review searching first (We have already covered
it.)
Searching
Searching
• Given a list of data find the location of a particular value
or report that value is not present
• linear search
• intuitive approach
• start at first item
• is it the one I am looking for?
• if not go to next item
• repeat until found or all items checked
• If items not sorted or unsortable this approach is
necessary
Attendance Question 1
• What is the average case Big O of linear search in an
array with N items, if an item is present?
A. O(N)
B. O(N2)
C. O(1)
D. O(logN)
E. O(NlogN)
Searching in a Sorted List
• If items are sorted then we can divide and conquer
• dividing your work in half with each step
• generally a good thing
• The Binary Search on List in Ascending order
• Start at middle of list
• is that the item?
• If not is it less than or greater than the item?
• less than, move to second half of list
• greater than, move to first half of list
• repeat until found or sub list size = 0
Attendance Question 2

What is the worst case Big O of binary search in an array with N items, if an
item is present?
A. O(N)
B. O(N2)
C. O(1)
D. O(logN)
E. O(NlogN)
Sorting
• A fundamental application for computers
• Done to make finding data (searching) faster
• Many different algorithms for sorting
• One of the difficulties with sorting is working with a fixed
size storage container (array)
• if resize, that is expensive (slow)
• The "simple" sorts run in quadratic time O(N2)
• bubble sort
• selection sort
• insertion sort
Sorting Fun
Why Not Bubble Sort?
Bubble Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Bubble Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Bubble Sort
5 1 3 4 2 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 3 4 2 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 3 2 4 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 3 2 4 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
5 1 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 5 2 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 5 3 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 5 4 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Bubble Sort
template<class ItemType>
void Swap(ItemType& item1, ItemType& item2)
{
ItemType tempItem;

tempItem = item1;
item1 = item2;
item2 = tempItem;
}
Bubble Sort
template<class ItemType>
void BubbleUp(ItemType values[] , int startIndex, int endIndex)
{
for (int index = endIndex; index > startIndex; index--)
if (values[index-1] > values[index])
Swap(values[index-1], values[index]);
}

template<class ItemType>
void BubbleSort(ItemType values[], int numValues)
{
int current = 0;

while (current < numValues - 1)


{
BubbleUp(values, current, numValues-1);
current++;
}
}
Bubble Sort
template<class ItemType>
void BubbleUp(ItemType values[] , int startIndex, int endIndex)
{
for (int index = endIndex; index > startIndex; index--)
if (values[index-1] > values[index])
Swap(values[index-1], values[index]);
}

template<class ItemType>
void BubbleSort(ItemType values[], int numValues)
{
int current = 0;

while (current < numValues - 1) O(N)


{
BubbleUp(values, current, numValues-1);
current++;
O(N)
}
}
O(N2)
Bubble Sort (little improved)
template<class ItemType>
void BubbleUp2(ItemType values[], int startIndex, int endIndex, bool&
sorted)
{
sorted = true;
for (int index = endIndex; index > startIndex; index--)
if (values[index] < values[index-1])
{
Swap(values[index], values[index-1]);
sorted = false;
}
}
template<class ItemType>
void ShortBubble(ItemType values[], int numValues)
{
int current = 0;
bool sorted = false;
while (current < numValues - 1 && !sorted)
{
BubbleUp2(values, current, numValues-1, sorted);
current++;
}
}
Selection Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
5 1 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2

Current

Comparison

Data Movement

Sorted
Selection Sort
1 5 3 4 6 2
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5

Current

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 6 5
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 5 6
 
Current Smallest

Comparison

Data Movement

Sorted
Selection Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Selection Sort
template<class ItemType>
int getMinIndex(ItemType values[], int startIndex, int endIndex)
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
template<class ItemType>
void SelectionSort(ItemType values[], int numValues)
{
int endIndex = numValues-1;
int minIndex;
for (int current = 0; current < endIndex; current++)
{
minIndex = getMinIndex(values, current, endIndex);
Swap(values[current], values[minIndex]);
}
}
Selection Sort
template<class ItemType>
int getMinIndex(ItemType values[], int startIndex, int endIndex)
{
int indexOfMin = startIndex;
for (int index = startIndex + 1; index <= endIndex; index++)
if (values[index] < values[indexOfMin])
indexOfMin = index;
return indexOfMin;
}
template<class ItemType>
void SelectionSort(ItemType values[], int numValues)
{
int endIndex = numValues-1;
int minIndex;
for (int current = 0; current < endIndex; current++)O(N)
{
O(N)
minIndex = getMinIndex(values, current, endIndex);
Swap(values[current], values[minIndex]);
}
}
O(N2)
Insertion Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
5 1 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 5 3 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 5 4 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 6 2

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 2 6

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 5 2 6

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 2 5 6

Comparison

Data Movement

Sorted
Insertion Sort
1 3 4 2 5 6

Comparison

Data Movement

Sorted
Insertion Sort
1 3 2 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort
1 3 2 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort
1 2 3 4 5 6

Comparison

Data Movement

Sorted
Insertion Sort
template<class ItemType>
void InsertItem(ItemType values [], int startIndex, int endIndex)
{
bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);
while (moreToSearch && !finished)
{
if (values[current] < values[current -1] )
{
Swap(values[current], values[current-1]);
current--;
moreToSearch = (current != startIndex);
}
else finished = true;
}
)
template<class ItemType>
void InsertionSort(ItemType values[], int numValues)
{
for (int count = 0; count < numValues; count++)
InsertItem(values, 0, count);
}
Insertion Sort
template<class ItemType>
void InsertItem(ItemType values [], int startIndex, int endIndex)
{
bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);
while (moreToSearch && !finished)
{
if (values[current] < values[current -1] )
{
Swap(values[current], values[current-1]);
current--;
moreToSearch = (current != startIndex);
}
else finished = true;
}
)
template<class ItemType>
void InsertionSort(ItemType values[], int numValues)
{
for (int count = 0; count < numValues; count++)
InsertItem(values, 0, count); O(N ) 2

}
Merge Sort Algorithm
Don Knuth cites John von Neumann as the creator
of this algorithm
1. If a list has 1 element or 0 elements it
is sorted
2. If a list has more than 2 split into into 2
separate lists
3. Perform this algorithm on each of
those smaller lists
4. Take the 2 sorted lists and merge them
together
Merge Sort
Final Comments
• Language libraries often have sorting algorithms in them
• Java Arrays and Collections classes
• C++ Standard Template Library
• Python sort and sorted functions
• Hybrid sorts
• when size of unsorted list or portion of array is small use
insertion sort, otherwise use O(Nlog N) sort like Quicksort of
Mergesort
• Many other sorting algorithms exist.

You might also like