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

Additional Example of Selection and Insertion Sort

The selection sort algorithm works by selecting the smallest unsorted item remaining in the list and swapping it with the item in the next position to be filled. It divides the array into a sorted and unsorted part, finds the minimum element in the unsorted part and adds it to the sorted part by swapping. This process is repeated until the unsorted part is empty, sorting the entire array. It has a time complexity of O(n^2) and is more efficient than bubble sort but less so than insertion sort.

Uploaded by

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

Additional Example of Selection and Insertion Sort

The selection sort algorithm works by selecting the smallest unsorted item remaining in the list and swapping it with the item in the next position to be filled. It divides the array into a sorted and unsorted part, finds the minimum element in the unsorted part and adds it to the sorted part by swapping. This process is repeated until the unsorted part is empty, sorting the entire array. It has a time complexity of O(n^2) and is more efficient than bubble sort but less so than insertion sort.

Uploaded by

wube hailu
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Chapter Three

Simple Sorting and


Searching Algorithms

1
Sorting Algorithm
Simple Sorting ------------ done
Bubble Sorting ----------- done
Selection Sorting
Insertion Sorting

2
III. Selection Sort
Algorithm

 The selection sort algorithm is in many ways similar to

simple sort algorithms.

 The idea of algorithm is quite simple. Array is imaginary

divided into two parts - sorted one and unsorted one.

 At the beginning, sorted part is empty, while unsorted one

contains whole array.

 At every step, algorithm finds minimal element in the

unsorted part and adds it to the end of the sorted one.


3

 When unsorted part becomes empty, algorithm stops.


 Works by selecting the smallest unsorted item remaining in the
list, and then swapping it with the item in the next position to
be filled.

 Similar to the more efficient insertion sort.

 It yields a 60% performance improvement over the bubble


sort.
Advantage:
 Simple and easy to implement.
Disadvantage:
 Inefficient for larger lists.
4
Implementation:

void selectionSort(int list[ ] ) {


      int minIndex, temp;    
      for (int i = 0; i <= n - 2; i++) {
            minIndex = i;
           for (j = i + 1; j <= n-1; j++)
                  if (list[j] < list[minIndex])
                        minIndex = j;
            if (minIndex != i) {
                  temp = list[i];
                  list[i] = list[minIndex];
                  list[minIndex] = temp;
            }
      } 5

}
Example: i=0,
j=1
Index =0

i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5

ind j
ex

If( ArrayList[j] <ArrayList[index])  True then


index=j

6
Example: i=0,
j=2
Index =1

i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5

j
ind
ex

If( ArrayList[j] <ArrayList[index])  False


Noting Doing

7
Example: i=0,
j=3
Index =1

i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5

ind j
ex

If( ArrayList[j] <ArrayList[index])  False


Noting Doing

8
Example: i=0,
j=4
Index =1

i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5

ind
ex
j

If( ArrayList[j] <ArrayList[index])  False


Noting Doing

9
Example: i=0,
j=5
Index =1

i
Value 5 1 3 4 6 2
Index 0 1 2 3 4 5

ind
ex
j

If( ArrayList[j] <ArrayList[index])  False


Noting Doing

10
Example: i=0,
j=6
Index =1

i
Value 5 1 3 4 6 2 Over
Index 0 1 2 3 4 5

ind j
ex

Swap (ArrayList[i], ArrayList[index])

Value 1 5 3 4 6 2
Index 0 1 2 3 4 5

11
Example: i=1,
j=2
Index =1

i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5

ind j
ex

If( ArrayList[j] <ArrayList[index])  True then


index=j

12
Example: i=1,
j=3
Index =2

i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5

j
ind
ex

If( ArrayList[j] <ArrayList[index])  False


Nothing Doing

13
Example: i=1,
j=4
Index =2

i
Value 1 5 3 4 6 2
Index 0 1 2 3 4 5

ind j
ex

If( ArrayList[j] <ArrayList[index])  False


Nothing Doing

14
Example: i=1,
j=5
Index =1

Value 1 5 3 4 6 2
Index 0 1 2 3 4 5

ind j
ex

If( ArrayList[j] <ArrayList[index])  True


index=j

15
Example: i=1,
j=6
Index =5

i
Value 1 5 3 4 6 2 Over
Index 0 1 2 3 4 5

j
ind
ex

Swap(ArrayList[i], ArrayList[index])

Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
16
Example: i=2,
j=3
Index =2

i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5

ind j
ex

if(ArrayList[j]< ArrayList[index]) => False


Nothing Doing

17
Example: i=2,
j=4
Index =2

i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5

ind j
ex

if(ArrayList[j]< ArrayList[index]) => False


Nothing Doing

18
Example: i=2,
j=3
Index =2

i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5

ind j
ex

Swap(ArrayList[i], ArrayList[index])

Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
19
Example: i=3,
j=4
Index =3

Value 1 2 3 4 6 5
Index 0 1 2 3 4 5

ind j
ex

if(ArrayList[j]< ArrayList[index]) => False


Nothing Doing

20
Example: i=3,
j=5
Index =5

Value 1 2 3 4 6 5
Index 0 1 2 3 4 5

ind j
ex

if(ArrayList[j]< ArrayList[index]) => False


Nothing Doing

21
Example: i=3,
j=6
Index =3

i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5

ind j
ex

Swap(ArrayList[i], ArrayList[index])

Value 1 2 3 4 6 5
Index 0 1 2 3 4 5
22
Example: i=4,
j=5
Index =4

i
Value 1 2 3 4 6 5
Index 0 1 2 3 4 5

ind
ex
j

if(ArrayList[j]< ArrayList[index]) => True


index=j

23
Example: i=4,
j=6
Index =5

i
Value 1 2 3 4 6 5 Over
Index 0 1 2 3 4 5

j
ind
ex

Swap(ArrayList[i], ArrayList[index])

Value 1 2 3 4 5 6
Index 0 1 2 3 4 5
24
Example: i=5,
j=6
Index =5

i
Value 1 2 3 4 5 6 Over
Index 0 1 2 3 4 5

ind j
ex

Swap(ArrayList[i], ArrayList[index])

Value 1 2 3 4 5 6
Index 0 1 2 3 4 5
25
Example: i= 6
Over

Value 1 2 3 4 5 6
Index 0 1 2 3 4 5

Sorting is Done

26
Exercise
 Sort the following array list using selection Sort
algorithm (Show each steps)

8 10 9 5 4 1 2 3

27
Complexity Analysis

 Selection sort stops, when unsorted part becomes


empty.

 As we know, on every step number of unsorted


elements decreased by one.

 Therefore, selection sort makes n-1 steps (n is number


of elements in array) of outer loop, before stop.

 Every step of outer loop requires finding minimum in


unsorted part. Summing up, (n - 1) + (n - 2) + ... + 1,
results in O(n2) number of comparisons. 28
 Number of swaps may vary from zero (in case of
sorted array) to n-1 (in case array was sorted in
reversed order), which results in O(n) number
of swaps.

 Overall algorithm complexity is O(n2).

 Fact, that selection sort requires n-1 number of


swaps at most, makes it very efficient in
situations, when write operation is significantly
more expensive, than read operation.

29
IV. Insertion Sort
Algorithm:
 Insertion sort algorithm somewhat be similar to
Selection Sort and Bubble sort.
 Array is imaginary divided into two parts - sorted one
and unsorted one.

 At the beginning, sorted part contains first element of


the array and unsorted one contains the rest.

 At every step, algorithm takes first element in the


unsorted part and inserts it to the right place of the
sorted one.

 When unsorted part becomes empty, algorithm stops.


30
Using binary search

 Itis reasonable to use binary search algorithm


to find a proper place for insertion.

 This optional of the insertion sort is called


binary insertion sort.

 After position for insertion is found,


algorithm shifts the part of the array and
inserts the element.

31
 Insertion sort works by inserting item into its proper
place in the list.
 Insertion sort is simply like playing cards: To sort the
cards in your hand, you extract a card, shift the
remaining cards and then insert the extracted card in
the correct place.
 This process is repeated until all the cards are in the
correct sequence.
 Is over twice as fast as the bubble sort and is just as
easy to implement as the selection sort.
Advantage:
 Relatively simple and easy to implement.
Disadvantage:
 Inefficient for large lists.

32
C++ implementation

void InsertionSort(int list[])


{
for(int i=1; i<=9; i++){
int j=i;
int temp=list[j];
while(list[j-1]>temp && j>=1){
list[j]=list[j-1];
j--;
}
list[j]=temp;
}
}
33
Insertion Sort Example

Step 1: - Pick 12
Put at first position

12
10
6 15 8 3
1 2 34
Insertion Sort Example

 Step 2: - Pick 10
 10 is less than 12
 Shift to right 12 and
put 10 first
12

10 6 15 8 3 1 2
35
Insertion Sort Example

 Step 3: - Pick 6
 6 is less than all
then shift 10 and 12.
and then put 6 at
first
0 12
1

6 15
8 3 1
2
36
Insertion Sort Example

 Step 4: - Pick 15
 15 is greater than all
then put it at the
end

0 12
1
6

15 8 3 1
2
37
Insertion Sort Example

 Step 5: - Pick 8
 8 is greater than 6
and less than 10.
then shift 10, 12
2 15 1nd 15 and put b/n 6
1
10 and 10
6

8 3 1
2
38
Insertion Sort Example

 Step 6: - Pick 3
 3 is less than 6 then
shift 15,12,10,8 and
6 and put 3 before 6

1
10 1122 5
8
6

3 1 2
39
Insertion Sort Example

 Step 8: - Pick 1
 1 is less than all
then shift 15, 12,
10, 8, 6 and 3 and
put 1 before 3
1
10 1122 5
8
6
3

1 2
40
Insertion Sort Example

 Step 9: - Pick 2
 2 is less than 3 and greater
1 than then shift 15, 12,
10, 8, 6 and 3 and put b/n
1 and 3

8 10 1122 15
6
3
1

2 41
Insertion Sort Example

 Finally, we become to the


sorted one

8 10 1122 15
6
3
2
1

42
Exercise : using insertion sort

7 -5 2 16 4

43
Complexity Analysis

 The complexity of insertion sorting is O(n) at


best case of an already sorted array and  O(n2) at
worst case, regardless of the method of
insertion.

 Number of comparisons may vary depending on


the insertion algorithm.

 O(n2) for shifting or swapping methods.


 O(nlogn) for binary insertion sort.

44

You might also like