



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
THIS DOC IS FOR SORTING WITH THEIR DIFFERENT METHODS. BUBBLE SORT WITH PROGRAM INSERTION SORT WITH PROGRAM SELECTION SORT WITH PROGRAM EACH AND EVERY POINT EXPLAINED IN DETAIL. CHECK IT OUT FOR A VERY REASONABLE PRICE .
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
ascending or descending order. For example, an unsorted array a[5] = {3, 10, 11, 2, 1} becomes sorted in ascending order as a[5] = {1, 2, 3, 10, 11}.
Bubble Sort
Bubble sort is a very simple method that sorts array elements by repeatedly moving the largest element to the highest index position of the array segment. In bubble sort, consecutive adjacent elements in the array are compared with each other. If the element at the lower index is greater than the element at the higher index, the two elements are swapped.
PROGRAM:-
#include <stdio.h> int main() { int a[5] = {5, 2, 15, 22, 1}; int i, j, temp; for (i = 4; i > 0; i--) { for (j = 0; j <= i - 1; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;
printf("The sorted array is = "); for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } return 0; }
Bubble Sort Algorithm Steps:
Selection Sort
The following table is an example: 10, 4, 6, 3, 2 0, 1, 2, 3, 4 (index position) For i = 0 (i <= 4 or i < 5)
min = a[0] = 10
To travel elements: j = i + 1, j <= 4
If a[j] < a[min]
For j = 1: a[1] < a[0] => 4 < 10 (True). min = a[1]
■ Assume min = i
■ For (j = i + 1; j <= 4; j++)
■ If a[j] < a[min]
■ min = j
○ a[i] = a[min]
○ a[min] = temp
Insertion Sort :-
Example: a[5] = {10, 4, 6, 3, 2}
#include <stdio.h> int main() { int a[5] = {10, 4, 6, 3, 2}; int i, j, temp;
for (i = 1; i < 5; i++) { // store element in variables of temp temp = a[i]; for (j = i; j > 0 && temp < a[j-1]; j--) { // vacant space a[j] = a[j-1]; } a[j] = temp; }
for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } return 0; }
Explanation/Definition: Insertion sort is a simple sorting algorithm that builds a sorted array one element at a time.