0% found this document useful (0 votes)
2 views3 pages

Sorting Algorithms Flowcharts

The document provides detailed notes on three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm is explained with its idea, algorithm steps, examples, and complexity analysis. Additionally, a comparison highlights their stability, efficiency, and performance on large data sets.
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)
2 views3 pages

Sorting Algorithms Flowcharts

The document provides detailed notes on three sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each algorithm is explained with its idea, algorithm steps, examples, and complexity analysis. Additionally, a comparison highlights their stability, efficiency, and performance on large data sets.
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

Bubble Sort, Selection Sort, and Insertion Sort — Full Notes

1. Bubble Sort

Idea:

Repeatedly swap adjacent elements if they are in the wrong order.

Algorithm:

1. For i from 0 to n-1

2. For j from 0 to n-i-2

3. If A[j] > A[j+1], swap them

Example:

Initial: [5, 2, 9, 1]

Pass 1: [2, 5, 1, 9]

Pass 2: [2, 1, 5, 9]

Pass 3: [1, 2, 5, 9]

Complexity:

Worst: O(n^2), Best: O(n), Space: O(1)

------------------------------------------------------------

2. Selection Sort

Idea:

Select the smallest element and place it at the correct index each pass.

Algorithm:

1. For i from 0 to n-1

2. min_index = i

3. For j from i+1 to n-1


4. If A[j] < A[min_index], update min_index

5. Swap A[i], A[min_index]

Example:

Initial: [64, 25, 12, 22, 11]

After pass 1: [11, 25, 12, 22, 64]

After pass 2: [11, 12, 25, 22, 64]

After pass 3: [11, 12, 22, 25, 64]

Complexity:

Always O(n^2), Space: O(1)

------------------------------------------------------------

3. Insertion Sort

Idea:

Insert each element into its correct position in the sorted part of the array.

Algorithm:

1. For i from 1 to n-1

2. key = A[i]

3. j = i - 1

4. While j>=0 and A[j] > key:

5. A[j+1] = A[j]

6. j--

7. Insert key at A[j+1]

Example:

Initial: [5, 2, 4, 6, 1]
Result: [1, 2, 4, 5, 6]

Complexity:

Worst: O(n^2), Best: O(n), Space: O(1)

------------------------------------------------------------

Comparison

Bubble Sort: stable, simple, inefficient on large data

Selection Sort: not stable, always O(n^2), minimal swaps

Insertion Sort: stable, efficient for nearly sorted arrays

You might also like