0% found this document useful (0 votes)
40 views

Schoolboek-Data Structures and Algorithms in Java

The document provides information about sorting algorithms, specifically Shell sort. It discusses the learning objectives, an overview of Shell sort including how it improves on insertion sort by comparing elements farther apart, and provides a step-by-step example of implementing Shell sort on a sample data set. The example shows the process of sorting the data set over multiple passes with decreasing gaps between compared elements.

Uploaded by

Zinko Koyell
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Schoolboek-Data Structures and Algorithms in Java

The document provides information about sorting algorithms, specifically Shell sort. It discusses the learning objectives, an overview of Shell sort including how it improves on insertion sort by comparing elements farther apart, and provides a step-by-step example of implementing Shell sort on a sample data set. The example shows the process of sorting the data set over multiple passes with decreasing gaps between compared elements.

Uploaded by

Zinko Koyell
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 57

University of Computer Studies

Advanced Sorting

Daw Su Su Win

Lecturer

Faculty of Computer Science

Data Structures and Algorithms


University of Computer Studies, FCS 1
Learning Objectives FCS

 To learn about the sorting algorithm

 To understand how to work Shell sort, Quick Sort and


Radix Sort algorithm

University of Computer Studies, FCS 2


1.Shell Sort FCS

 Shell sort is a generalization of the insertion sort.

 It was introduced to improve the efficiency of simple insertion sort.

 Although compare elements that are adjacent in insertion sort, shell sort compare
elements with are a distant part.
 Uses incremental i, sometimes called the gap or interval to create a sub-list.

 In each pass, the value of gap till reaching the last pass when a gap is 1.

 Shell sort is like insertion sort in the last pass.

University of Computer Studies, FCS 3


Step by Step Process of Shell Sort FCS

1. Count the number of elements (n) in a given array.


2. Divide the list into , ,…… sub-lists. (ignore the reminder if n is odd)
3. Sort each sub-list using shuttle sort.
4. Merge the sub-lists.

- Repeat steps 2 and 3 until the number of sub-list is 1.


- Elements are compared with less than(<), equal (=) or greater than (>) in cases where strings may
be applied.

University of Computer Studies, FCS 4


Shell Sort (Example) FCS

Consider the following unsorted array with elements 15, 17, 20, 35, 25, 90, 27, 30, 11.
There are total of 9 elements, n=9

array index 0 1 2 3 4 5 6 7 8
elements 15 17 20 35 25 90 27 30 11

Find a gap,
gap = floor(n/2) for pass 1
=floor (9/2) gap= 4
=floor(4.5)
=4
University of Computer Studies, FCS 5
Shell Sort (Example) FCS

Pass 1
gap=4

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

First element at index 0


Next element’s index=0+4=4
Third element’s index=4+4=8

University of Computer Studies, FCS 6


Shell Sort (Example) FCS

Pass 1
gap=4

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Similarly, element at index 1


Next element’s index=1+4=5
Third element’s index=5+4=9
There is no 9th index in the above array.
So, index is 1 and 5
University of Computer Studies, FCS 7
Shell Sort (Example) FCS

Pass 1
gap=4

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Similarly, element at index 2


Next element’s index=2+4=6
Third element’s index=6+4=10
There is no 10th index in the above array.
So, index is 2 and 6
University of Computer Studies, FCS 8
Shell Sort (Example) FCS

Pass 1
gap=4

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Similarly, element at index 3


Next element’s index=3+4=7
Third element’s index=7+4=11
There is no 11st index in the above array.
So, index is 3 and 7
University of Computer Studies, FCS 9
Shell Sort (Example) FCS

Pass 1
gap=4

sub-list 1 sub-list 2 sub-list 3 sub-list 4

end

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Compare elements 15>25 (NO)


Shift one position towards right hand side
University of Computer Studies, FCS 10
Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Compare elements 17>90 (NO)


Shift one position towards right hand side

University of Computer Studies, FCS 11


Shell Sort (Example) FCS

Pass 1
end
gap=4

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Compare elements 20>27 (NO)


Shift one position towards right hand side

University of Computer Studies, FCS 12


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 35 25 90 27 30 11

Compare elements 35>30 (YES)


Swap them

University of Computer Studies, FCS 13


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 30 25 90 27 35 11

Compare elements 35>30 (YES)


Swap them
Then, Shift one position towards right hand side

University of Computer Studies, FCS 14


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 30 25 90 27 35 11

Compare elements 25>11 (YES)


Swap them

University of Computer Studies, FCS 15


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 30 11 90 27 35 25

Compare elements 25>11 (YES)


Swap them
In this sub-list, compare three elements then put the elements in correct position.

University of Computer Studies, FCS 16


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
15 17 20 30 11 90 27 35 25

Compare elements 15>11 (YES)


Swap them

University of Computer Studies, FCS 17


Shell Sort (Example) FCS

Pass 1
gap=4 end

0 1 2 3 4 5 6 7 8
11 17 20 30 15 90 27 35 25

Compare elements 15>11 (YES)


Swap them
If the last index reaches the end of the array, pass 1 is completed and move to pass 2

University of Computer Studies, FCS 18


Shell Sort (Example) FCS

Pass 2

0 1 2 3 4 5 6 7 8
11 17 20 30 15 90 27 35 25

Find a gap,
gap = floor(gap/2) for pass 2
=floor (4/2) gap= 2
=floor(2)
=2

University of Computer Studies, FCS 19


Shell Sort (Example) FCS

Pass 2
gap=2

sub-list 1 sub-list 2

end

0 1 2 3 4 5 6 7 8
11 17 20 30 15 90 27 35 25

Compare elements 11>20 (NO)


Shift one position towards right hand side
University of Computer Studies, FCS 20
Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 20 30 15 90 27 35 25

Compare elements 17>30 (NO)


Shift one position towards right hand side

University of Computer Studies, FCS 21


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 20 30 15 90 27 35 25

Compare elements 20>15 (YES)


Swap them

University of Computer Studies, FCS 22


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 90 27 35 25

Compare elements 20>15 (YES)


Swap them
Check the other pair left side of end

University of Computer Studies, FCS 23


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 90 27 35 25

Compare elements 11>15 (NO)


Shift one position towards right hand side and next the element of gap 2

University of Computer Studies, FCS 24


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 90 27 35 25

Compare elements 30>90 (NO)


Shift one position towards right hand side and next the element of gap 2

University of Computer Studies, FCS 25


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 90 27 35 25

Compare elements 20>27 (NO)


Shift one position towards right hand side and next the element of gap 2

University of Computer Studies, FCS 26


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 90 27 35 25

Compare elements 90>35 (Yes)


Swap them

University of Computer Studies, FCS 27


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35
90 27 90 25

Compare elements 90>35 (Yes)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 28


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35
90 27 90 25

Compare elements 30>35 (NO)


Shift one position towards right hand side and next the element of gap 2

University of Computer Studies, FCS 29


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35
90 27 90 25

Compare elements 27>25 (YES)


Swap them

University of Computer Studies, FCS 30


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35
90 25 90 27

Compare elements 27>25 (YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 31


Shell Sort (Example) FCS

Pass 2
gap=2 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35
90 25 90 27

Compare elements 20>25 (NO)


If the last index reaches the end of the array, pass 2 is completed and move to pass 3

University of Computer Studies, FCS 32


Shell Sort (Example) FCS

Pass 3

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35 25 90 27

Find a gap,
gap = floor(gap/2) for pass 3
=floor (2/2) gap= 1
=floor(1)
=1
When gap =1
Shell sort is like insertion sort University of Computer Studies, FCS 33
Shell Sort (Example) FCS

Pass 3
gap=1

sub-list 1
end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35 25 90 27

Compare elements 11>17 (NO)


Shift one position towards right hand side and next the element of gap 1

University of Computer Studies, FCS 34


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 17 15 30 20 35 25 90 27

Compare elements 17>15 (YES)


Swap them

University of Computer Studies, FCS 35


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 30 20 35 25 90 27

Compare elements 17>15 (YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 36


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 30 20 35 25 90 27

Compare elements 11>15 (NO)


Shift one position towards right hand side and next the element of gap 2

University of Computer Studies, FCS 37


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 30 20 35 25 90 27

Compare elements 17>30 (NO)


Shift one position towards right hand side and next the element of gap 1

University of Computer Studies, FCS 38


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 30 20 35 25 90 27

Compare elements 30>20(YES)


Swap them

University of Computer Studies, FCS 39


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 30 35 25 90 27

Compare elements 30>20(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 40


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 30 35 25 90 27

Compare elements 30>35 (NO)


Shift one position towards right hand side and next the element of gap 1

University of Computer Studies, FCS 41


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 30 35 25 90 27

Compare elements 35>25(YES)


Swap them

University of Computer Studies, FCS 42


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 30 25 35 90 27

Compare elements 35>25(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 43


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 30 25 35 90 27

Compare elements 30>25(YES)


Swap them

University of Computer Studies, FCS 44


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 90 27

Compare elements 30>25(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 45


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 90 27

Compare elements 20>25(NO)


Shift one position towards right hand side and next the element of gap 1

University of Computer Studies, FCS 46


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 90 27

Compare elements 35>90(NO)


Shift one position towards right hand side and next the element of gap 1

University of Computer Studies, FCS 47


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 90 27

Compare elements 90>27(YES)


Swap them

University of Computer Studies, FCS 48


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 27 90

Compare elements 90>27(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 49


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 35 27 90

Compare elements 35>27(YES)


Swap them

University of Computer Studies, FCS 50


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 27 35 90

Compare elements 35>27(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 51


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 30 27 35 90

Compare elements 30>27(YES)


Swap them

University of Computer Studies, FCS 52


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 27 30 35 90

Compare elements 30>27(YES)


Swap them
Check the other pair left side end

University of Computer Studies, FCS 53


Shell Sort (Example) FCS

Pass 3
gap=1 end

0 1 2 3 4 5 6 7 8
11 15 17 20 25 27 30 35 90

Compare elements 25>27(NO)


If the last index reaches the end of the array, we have ascending order sorted array.

University of Computer Studies, FCS 54


Application of Shell Sort FCS

 Linux kernel

University of Computer Studies, FCS 55


Summary FCS

 Step by step process of Shell sort

University of Computer Studies, FCS 56


Learning Outcome FCS

After learning this lecture, the student will be able to:


 Implement the sorting application using Shell sort,

University of Computer Studies, FCS 57

You might also like