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

Sorting

Sorting is the process of arranging elements in a specific order, with common types including Bubble Sort, Selection Sort, and Insertion Sort. Bubble Sort compares adjacent elements and swaps them if they are in the wrong order, while Selection Sort repeatedly selects the smallest element from the unsorted part and places it in the sorted section. Insertion Sort builds the sorted list one element at a time by comparing and inserting elements into their correct position.

Uploaded by

alonatgeorge
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 views3 pages

Sorting

Sorting is the process of arranging elements in a specific order, with common types including Bubble Sort, Selection Sort, and Insertion Sort. Bubble Sort compares adjacent elements and swaps them if they are in the wrong order, while Selection Sort repeatedly selects the smallest element from the unsorted part and places it in the sorted section. Insertion Sort builds the sorted list one element at a time by comparing and inserting elements into their correct position.

Uploaded by

alonatgeorge
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

SORTING

Sorting is the process of arranging elements in a particular order (ascending, descending,


alphabetical, etc.).
Types of sorting:
1. Bubble sort
2. Selection sort
3. Insertion sort

1. Bubble sort: Bubble Sort is a simple comparison-based sorting algorithm in which


each pair of adjacent elements is compared, and the elements are swapped if they are
in the wrong order.

Working Principle:
In each pass, the largest element “bubbles up” to the end of the list. This process is
repeated for all elements until the list is completely sorted.
Steps:
1. Compare adjacent elements.
2. Swap them if the first is greater than the second.
3. Repeat for all elements.
4. After each pass, the largest element moves to its correct position.
5. Continue until no swaps are needed.
Example
Algorithm: BUBBLESORT( numList, n)
Step 1: SET i = 0
Step 2: WHILE i< n REPEAT STEPS 3 to 8
Step 3: SET j = 0
Step 4: WHILE j< n-i-1, REPEAT STEPS 5 to 7
Step 5: IF numList[j] > numList[j+1] THEN
Step 6: swap(numList[j],numList[j+1])
Step 7: SET j=j+1
Step 8: SET i=i+1

2. SELECTION SORT:
Definition:
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest (or
largest) element from the unsorted part of the list and places it at the correct position
in the sorted part.
Working Principle:
In each pass, the algorithm finds the minimum element from the unsorted section
and swaps it with the first unsorted element. This process continues until the entire
list is sorted.
Steps:
1. Start from the first element.
2. Find the smallest element in the remaining list.
3. Swap it with the first element.
4. Move to the next position and repeat until the list is sorted.
Example:
3. Insertion sort:
Definition:
Insertion Sort is a simple sorting algorithm that builds the final sorted list one element
at a time. It works the same way as sorting playing cards in your hands.

Steps / Working:
1. Assume the first element is already sorted.
2. Pick the next element (called the key) from the unsorted part.
3. Compare the key with elements in the sorted part.
4. Shift all larger elements one position to the right.
5. Insert the key into its correct position.
6. Repeat the process for all elements until the list is sorted.
Example:

You might also like