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

Unit 12_13.1 Pr

The document discusses three sorting techniques: Selection Exchange Sort, Bubble Sort, and Shell Sort, detailing their algorithms, steps, and time complexities. It also outlines criteria for choosing the most efficient sorting algorithm based on data size, order, memory constraints, and stability requirements. Additionally, it provides examples of comparisons and exchanges made during sorting an array using each technique.

Uploaded by

ziko ALH
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)
5 views7 pages

Unit 12_13.1 Pr

The document discusses three sorting techniques: Selection Exchange Sort, Bubble Sort, and Shell Sort, detailing their algorithms, steps, and time complexities. It also outlines criteria for choosing the most efficient sorting algorithm based on data size, order, memory constraints, and stability requirements. Additionally, it provides examples of comparisons and exchanges made during sorting an array using each technique.

Uploaded by

ziko ALH
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/ 7

Week 13 – Lecture 1 (Practical)

Problem Solving in Computing

Sorting

1) Explain the technique used in the:


a. Selection Exchange Sort
b. Bubble Sort
c. Shell Sort

a. Selection Exchange Sort

Selection Exchange Sort is a simple comparison-based sorting technique. It divides


the array into two parts: the sorted part and the unsorted part. The algorithm
repeatedly selects the smallest (or largest) element from the unsorted part and swaps
it with the first element of the unsorted part.

Steps:

1. Find the smallest element in the unsorted part of the array.

2. Swap it with the leftmost unsorted element.

3. Move the boundary of the sorted section one element to the right.

4. Repeat until the array is sorted.

Time Complexity:

- Best, Average, and Worst Case: O(n^2)

b. Bubble Sort

Bubble Sort repeatedly compares adjacent elements in the array and swaps them if
they are in the wrong order. Larger elements 'bubble up' to the end of the array in each
iteration.

Steps:

1. Compare adjacent elements and swap them if needed.

2. Pass through the array repeatedly, ignoring the last sorted elements in each
subsequent pass.

3. Continue until no swaps are needed.

Time Complexity:

- Best Case: O(n)


- Worst and Average Case: O(n^2)

c. Shell Sort

Shell Sort is an optimization of insertion sort. It starts by sorting elements far apart
and progressively reduces the gap between elements to be compared. The final
iteration performs a standard insertion sort.

Steps:

1. Define a gap sequence (e.g., n/2, n/4, ..., 1).

2. Perform a gapped insertion sort for each gap.

3. Decrease the gap until it becomes 1.

Time Complexity:

- Best Case: O(n log n)

- Worst Case: O(n^2)

2) How would you choose which sorting techniques are the most efficient for a set of data?

The choice of sorting algorithm depends on:


1. Data Size: Small arrays: Bubble or Selection sort (due to simplicity). Large arrays:
Shell sort or more advanced algorithms like Merge or Quick sort.
2. Data Order: Nearly sorted data: Bubble sort or Insertion sort (both perform well).
Random data: Shell sort or Quick sort.
3. Memory Constraints: Limited memory: Use in-place algorithms like Selection,
Bubble, or Shell sort.
4. Stability Requirement: Stability (maintaining relative order of equal keys): Bubble
sort.
3) Identify the comparisons and the exchanges made to sort the following array using the
selection exchange sort technique:

23
45
13
5
56
74
10
30
65
40

Passes:
1. Find the smallest element (5) and swap with the first element:

45

13

23

56

74

10

30

65

40

Comparisons: 9
Exchanges: 1
2. Find the next smallest (10) and swap with the second element:

10

13

23

56

74

45

30

65

40
Comparisons: 8
Exchanges: 1

3. Repeat for the rest of the array until sorted:


5

10

13

23

30

40

45

56

65

74

Total Comparisons: 45
Total Exchanges: 9

4) Identify the comparisons and the exchanges made to sort the array found in problem 3
using the bubble sort technique.
Initial Array:
23

45

13

56

74

10

30

65

40
Passes:
1. Compare adjacent elements, swapping if needed. After the first pass, the largest
element (74) is at the end:
Array:
23

13

45

56

10

30

65

40

74

Comparisons: 9
Exchanges: 4
2. Repeat for the remaining unsorted portion, with fewer comparisons each time:
Final Array:
5

10

14

23

30

40

45

56

65

74

Total Comparisons: 45
Total Exchanges: 20
5) Identify the comparisons and the exchanges made to sort the array found in problem 3
using the shell sort technique.
Initial Array:
23

45

13

56

74

10

30

65

40

Steps:
1. Choose an initial gap (e.g., 5):
Compare elements 5 positions apart. Perform gapped insertion sort.
Array:
10

30

13

40

23

74

45

65

56

Comparisons: 5
Exchanges: 3
2. Reduce the gap to 2:
Perform gapped insertion sort for gap 2.
Array:
5

10

13

23

30

40

45

56

65

74

Comparisons: 9
Exchanges: 4
3. Reduce the gap to 1 (standard insertion sort):
Sort normally.
Array:
5

10

13

23

30

40

45

56

65

74

Comparisons: 10
Exchanges: 0
Total Comparisons: 24
Total Exchanges: 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy