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

Sorting Algorithm Example Program

The document provides definitions, algorithms, and C programs for three sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort. Each sorting method is explained with a step-by-step algorithm and corresponding C code to implement the sorting. The document serves as a guide for understanding and coding these sorting algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Sorting Algorithm Example Program

The document provides definitions, algorithms, and C programs for three sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort. Each sorting method is explained with a step-by-step algorithm and corresponding C code to implement the sorting. The document serves as a guide for understanding and coding these sorting algorithms.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Bubble Sort

✅ Definition

Bubble Sort is a simple sorting technique in which adjacent elements are compared and
swapped if they are in the wrong order. After each pass, the largest element moves to the end.
✅ Algorithm (Bubble Sort)

1. Start
2. Read number of elements n
3. Read array A[0] to A[n-1]
4. For i = 0 to n-2
o For j = 0 to n-i-2
 If A[j] > A[j+1], swap them
5. Print sorted array
6. Stop
✅ C Program (Bubble Sort)
#include<stdio.h>
int main() {
int a[100], n, i, j, temp;

printf("Enter n: ");
scanf("%d",&n);

printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n-1;i++) {
for(j=0;j<n-i-1;j++) {
if(a[j] > a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

🔹 2. Selection Sort
✅ Definition

Selection Sort is a sorting method in which the smallest element is selected from the unsorted
part and placed at the beginning in each pass.
✅ Algorithm (Selection Sort)

1. Start
2. Read number of elements n
3. Read array A[0] to A[n-1]
4. For i = 0 to n-2
o Set min = i
o For j = i+1 to n-1
 If A[j] < A[min], set min = j
o Swap A[i] and A[min]
5. Print sorted array
6. Stop

✅ C Program (Selection Sort)


#include<stdio.h>
int main() {
int a[100], n, i, j, min, temp;

printf("Enter n: ");
scanf("%d",&n);

printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n-1;i++) {
min=i;
for(j=i+1;j<n;j++) {
if(a[j] < a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

🔹 3. Insertion Sort
✅ Definition

Insertion Sort is a sorting technique in which elements are picked one by one and inserted
into their correct position in the already sorted part of the array.
✅ Algorithm (Insertion Sort)

1. Start
2. Read number of elements n
3. Read array A[0] to A[n-1]
4. For i = 1 to n-1
o Set key = A[i]
o Set j = i-1
o While j >= 0 and A[j] > key
 Move A[j] to A[j+1]
 Decrease j
o Insert key at A[j+1]
5. Print sorted array
6. Stop

✅ C Program (Insertion Sort)


#include<stdio.h>
int main() {
int a[100], n, i, j, key;

printf("Enter n: ");
scanf("%d",&n);

printf("Enter elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=1;i<n;i++) {
key=a[i];
j=i-1;
while(j>=0 && a[j]>key) {
a[j+1]=a[j];
j--;
}
a[j+1]=key;
}

printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

You might also like