0% found this document useful (0 votes)
3 views29 pages

Lecture 07 - Sorting

The document is a lecture on sorting algorithms presented by Mrs. Mona Altassan for CS310 – Data Structure. It covers the concept of sorting, its importance, and various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort. The lecture also includes student activities to illustrate the sorting techniques discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views29 pages

Lecture 07 - Sorting

The document is a lecture on sorting algorithms presented by Mrs. Mona Altassan for CS310 – Data Structure. It covers the concept of sorting, its importance, and various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort. The lecture also includes student activities to illustrate the sorting techniques discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

+

CS310 – Data Structure


Lecture – 07 – Sorting
Mrs. Mona Altassan

March 5, 2020
+ 2

Objectives
◼ Idea of Sorting

◼ What is Sorting?

◼ Some algorithms of sorting:


◼ Bubble Sort

◼ Selection Sort

◼ Insertion Sort

◼ Merge Sort

◼ Quick Sort

March 5, 2020
+ 3

Idea of Sorting

March 5, 2020
+ 4

Recommended Readings

◼ Page – 531: Sorting - Chapter 13 - (Data Structure & Algorithm in

Java 6th Edition) By GoodRich

◼ Page – 255: Sorting- (Data Structure with Java) Schaum’s Outlines

March 5, 2020
+ 5

What is Sorting?

◼ Sorting is any process, where data in a list is organized in a


certain order either Ascending Or Descending Order,

◼ Example
◼ To arrange names in alphabetical order
◼ Arrange students by grade, etc.

A={3162134590} (unsorted)

A={0112334569} (sorted)

March 5, 2020
+ 6

Why do we need Sorting?

We need sorting techniques for main two purposes:


◼ Easier to understand and analyze data collection

◼ Searching process will be much faster

◼ Examples:
◼ Sorted in Ascending Order: Phone directory and dictionary

◼ Sorted in Descending Order: Student grades score in a class. The


topper get the highest score.

March 5, 2020
+ 7

Why do we need Sorting?

unsorted sorted into


Ascending order

March 5, 2020
+ 8

Sorting Process

Main activity in the sorting process:


◼ Compare: compare between two elements. If they are not in correct
order, then

◼ Swap: Change the position of the elements in order to get the right
order

◼ The efficiency of sorting algorithm is measured based on:


◼ The number of comparisons

◼ The number of swapping between elements


March 5, 2020
+ 9

Sorting Algorithms

◼ Quadratic Sorting Algorithms


◼ Bubble Sort

◼ Selection Sort

◼ Insertion Sort

◼ Divide and Conquer Sorting Algorithms


◼ Merge Sort

◼ Quick Sort
March 5, 2020
+ 10

Bubble Sort

Traverse a collection of elements


◼ Move from the front to the end

◼ “Bubble” the largest value to the end using pair-wise


comparisons and swapping

March 5, 2020
+ 11

Bubble Sort - Technique

◼ Given n numbers to sort:

◼ Repeat the following n-1 times:


◼ For each pair of adjacent numbers:
IF (Array[ index – 1 ] > Array[ index]) THEN

Swap (Array [ index -1] , Array [ index ]

End If

March 5, 2020
+ 12

Bubble Sort

March 5, 2020
+ 13

Bubble Sort – Student Activity


32 65 29 21 67 01

March 5, 2020
+ 14

Selection Sort

Traverse a collection of elements


◼ “Select” the smallest value from the unsorted group, then
put at the end of the sorted group using comparisons and
swapping

◼ Repeat the process until the unsorted group becomes


empty.

March 5, 2020
+ Selection Sort 15

March 5, 2020
+ 16

Selection Sort– Student Activity


32 65 29 21 67 01

March 5, 2020
+ Insertion Sort 17

◼ Sort the solitaire Cards ◼ Insert Jack in the cards sequence

▪ To insert J, we need to make room for it


by moving first King and then Queen

March 5, 2020
+ 18

Insertion Sort
◼ For each iteration
◼ insertion sort removes one element from the input
data
◼ finds the location it belongs within the sorted list,
and
◼ inserts it there.

◼ It repeats until no input elements remain unsorted

March 5, 2020
+ 19

Insertion Sort

March 5, 2020
+ 20

Insertion Sort– Student Activity


32 65 29 21 67 01

March 5, 2020
+ 21

Comparison

◼ Bubble sort is useful for small amounts of data.

◼ Selection
sort can be used when amount of data is small and
swapping is time-consuming.

◼ Insertion sort is most versatile and the best when the list is almost
sorted.

March 5, 2020
+ 22

Merge Sort

◼Traverse the collection by Divide-and-Conquer approach

◼ First divide the list into the smallest unit (1 element)

◼ then compare each element with the adjacent list to


sort and

◼ merge the two adjacent lists.

Finally all the elements are sorted and merged


March 5, 2020
+ 23

Merge Sort

March 5, 2020
+ 24

Merge Sort

◼ Merge sort works as follows:

◼ Divide the unsorted list into n sublists, each


containing 1 element
◼ (a list of 1 element is considered sorted).

◼ Repeatedly merge sublists by using


comparing to produce new sorted sublists

March 5, 2020
+ 25

Merge Sort– Student Activity


32 65 29 21 67 01

March 5, 2020
+ Quick Sort 26

◼ Based on divide-and-conquer approach

◼ Divides a large array into two smaller sub-


arrays: the low elements and the high
elements and

◼ pick a random element x (called pivot)


and partition the list

◼ then recursively sort the sub-arrays.

March 5, 2020
+ Quick Sort 27

◼ Divide: pick a random element x (called pivot)


and partition list into x

◼ L elements less than x

◼E elements equal x x
◼G elements greater than x
L E G
◼ Recursively: sort L and G

◼ Conquer: join L, E and G x

March 5, 2020
+ 28

Quick Sort– Student Activity


32 65 29 21 67 01

March 5, 2020
+ 29

End of Sorting – 09

March 5, 2020

You might also like