0% found this document useful (0 votes)
19 views18 pages

Lec - Array Sorting

Uploaded by

mianabdullah8856
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)
19 views18 pages

Lec - Array Sorting

Uploaded by

mianabdullah8856
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/ 18

DATA STRUCTURES AND

ALGORITHMS

Lecture: Array Sorting

Lecturer: Mohsin Abbas


National University of Modern Languages, Islamabad
SORTING
• Sorting takes an unordered collection and makes its an ordered one.
• Let A be a list of n elements A1, A2, … An in memory.
• Sorting A refers to the operation of rearranging the contents of A
• Ascending order
• Descending order

5 3 1 2 Sorting 1 2 3 5
Algorithm

2
SORTING
• In this lecture, we are going to look at three simple sorting
techniques:
• Bubble Sort
• Selection Sort
• Insertion Sort

3
BUBBLE SORT
• Traverse a collection of elements.
• Move an element from its position to the front (if smallest) or to the end (if
largest) in case of ascending order and vice versa.
• “Bubble” the value to the front or end using the operations
• Pair-wise comparison
• Swapping

7 2 8 5 4

4
BUBBLE SORT
• Compare each element (except the last one) with its neighbor to the right.
• If they are out of order, swap them
• This puts the largest element at the very end
• The last element is now in the correct and final place

• Compare each element (except the last two) with its neighbor to the right.
• If they are out of order, swap them
• This puts the second largest element next to last
• The last two elements are now in their correct and final places

• Compare each element (except the last three) with its neighbor to the right
• Continue as above until you have no unsorted elements on the left

5
BUBBLE SORT

7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8

2 7 8 5 4 2 5 7 4 8 2 4 5 7 8
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8

6
BUBBLE SORT
void BubbleSort ( int arr[], int size )
{
int temp;
for ( int outer = size-1; outer > 0; outer-- )
{
for ( int inner = 0; inner < outer;
inner++ )
{
if ( arr[inner] > arr[inner+1] )
{
temp = arr[inner];
arr[inner] = arr[inner+1];
arr[inner+1] = temp;
}
}
}
}
7
ANALYSIS OF BUBBLE SORT
for ( int outer = size-1; outer > 0; outer-- ) {
for ( int inner = 0; inner < outer; inner++ )
{
if ( arr[inner] > arr[inner+1] ) {
//code for swapping is
skipped } } }
• Let size = arr.length = size of the array
• The outer loop is executed n-1 times (call it n, that’s close enough)
• Each time the outer loop is executed, the inner loop is executed
• Inner loop executes n-1 times at first, linearly dropping to just once
• On average, inner loop executes about n/2 times for each execution of the outer loop
• In the inner loop, the comparison is always done (constant time), the swap might be done
(also constant time)
• Result is n * n/2 * k, that is, O(n2/2 + k) = O(n2)
8
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.

7 2 8 5 4

9
SELECTION SORT
• Given an array of length n,
• Search elements 0 through n-1 and select the smallest
• Swap it with the element in location 0
• Search elements 1 through n-1 and select the smallest
• Swap it with the element in location 1
• Search elements 2 through n-1 and select the smallest
• Swap it with the element in location 2
• Continue in this fashion until there’s nothing left to search

10
SELECTION SORT
void SelectionSort ( int arr[], int size )
{
7 2 8 5 4 int temp;
int min;
for ( int outer = 0; outer < size-1; outer++ )
2 7 8 5 4 {
min = outer;
for ( int inner = outer+1; inner < size; inner+
+ )
2 4 8 5 7 {
if ( arr[inner] < arr[min] )
{
2 4 5 8 7 min = inner;
}
}
temp = arr[outer];
2 4 5 7 8 arr[outer] = arr[min];
arr[min] = temp;
}
}
11
ANALYSIS OF SELECTION SORT
• The Selection Sort might swap an array element with itself--this is
harmless, and not worth checking for
• Analysis:
• The outer loop executes n-1 times
• The inner loop executes about n/2 times on average (from n to 2 times)
• Work done in the inner loop is constant (swap two array elements)
• Time required is roughly (n-1)*(n/2)
• You should recognize this as O(n2)

12
INSERTION SORT
• The list is divided into two parts: sorted and unsorted.
• In each pass, the following steps are performed:
• First element of the unsorted part (i.e., sub-list) is picked up
• Transferred to the sorted sub-list
• Inserted at the appropriate place
• A list of n elements will take at most n-1 passes to sort the data

13
INSERTION SORT
void InsertionSort ( int arr[], int size )
{
int temp;
for ( int outer = 1; outer < size; outer++ )
{
temp = arr[outer];
int inner = outer;
while ( inner > 0 && arr[inner-1] >=
temp )
{
arr[inner] = arr[inner-1];
inner--;
}
arr[inner] = temp;
}
}
14
INSERTION SORT

sorted next to be inserted

3 4 7 12 14 14 20 21 33 38 10 55 9 23 28 16
temp
less than 10 10

3 4 7 10 12 14 20
12 14 14 21 21 38
20 33 33 10
38 55 9 23 28 16

sorted

15
ANALYSIS OF INSERTION SORT
• We run once through the outer loop, inserting each of n elements;
this is a factor of n
• On average, there are n/2 elements already sorted
• The inner loop looks at (and moves) half of these
• This gives a second factor of n/2
• Hence, the time required for an insertion sort of an array of n
elements is proportional to n2/4
• Discarding constants, we find that insertion sort is O(n2)

16
CONCLUSION
• In this lecture we have studied:
• Bubble Sort
• Selection Sort
• Insertion Sort

17
Question?

18

You might also like