Program 9

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

PROGRAM 9

OBJECTIVE

Perform bubble sort, selection sort, and insertion sort.

THEORY

Sorting is arrangement/storage of data in the sorted order which can be ascending or descending.

Any sort algorithm that uses main memory exclusively during the sorting is known internal
sorting.

Internal sorting is faster than external sorting.

Bubble sort is a simple sorting technique which uses comparision based algorithm in which each
pair of adjacent elements are compared and are swapped if not in correct order. This technique is
not suitable for large data sets.

Selection is the simplest sorting algorithm which first finds the smallest element in the array and
swaps it with the first element of the array, and then finds the second smallest element in the
array and exchanges it with the element on the second position.

Insertion sort is not much efficient on large data than more advanced approaches like quick sort,
heap sort, or merge sort. In this technique we start by making the element at the index 1 as key.
We compare the key with the elements before it. If the key element is less than the first element,
we place it before the first element else if it is greater than the first element we place it after the
first element. We keep on repeating the process till the array is sorted.

PSEUDO CODE

ALGORITHM Swap(*xp, *yp)

BEGIN:

Temp=*xp

*xp=*yp

*yp=temp

END;
ALGORITHM BubbleSort(arr, n)

BEGIN:

FOR i = 0 TO i < n-1 DO

FOR j = 0 TO j < n-i-1 DO

IF arr[j] > arr[j+1] THEN

CALL Swap(&arr[j], &arr[j+1])

WRITE(Bubble Sort has been completed.)

END;

ALGORITHM SelectionSort(arr, n)

BEGIN:

min_idx=0

FOR i = 0 TO i < n-1 DO

min_idx = i

FOR j = i+1 TO j < n DO

IF arr[j] < arr[min_idx] THEN

min_idx = j

swap(&arr[min_idx], &arr[i])

WRITE(Selection Sort has been completed.)

END;

ALGORITHM InsertionSort(arr, n)

BEGIN:

key=0

FOR i = 1 TO i < n DO

key = arr[i]

j=i-1
WHILE j >= 0 AND arr[j] > key DO

arr[j + 1] = arr[j]

j=j-1

arr[j + 1] = key

WRITE (Insertion Sort has been completed.)

END;

SOURCE CODE

#include <stdio.h>

void swap(int *xp, int *yp) {

int temp = *xp;

*xp = *yp;

*yp = temp;

void BubbleSort(int arr[], int n) {

for (int i = 0; i < n-1; i++) {

for (int j = 0; j < n-i-1; j++) {

if (arr[j] > arr[j+1]) {

swap(&arr[j], &arr[j+1]);

printf("Bubble Sort has been completed. \n");


}

void SelectionSort(int arr[], int n) {

int i, j, min_idx;

for (i = 0; i < n-1; i++) {

min_idx = i;

for (j = i+1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

swap(&arr[min_idx], &arr[i]);

printf("Selection Sort has been completed. \n");

void InsertionSort(int arr[], int n) {

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;
}

arr[j + 1] = key;

printf("Insertion Sort has been completed. \n");

int main() {

int n, choice;

printf("Enter the size of an array: ");

scanf("%d", &n);

int arr[n];

printf("Enter the elements of an array: \n");

for(int i=0;i<n;i++){

scanf("%d", &arr[i]);

printf("Enter 1 for bubble sort, 2 for selection sort, and 3 for insertion sort");

scanf("%d",&choice);

switch (choice)

case 1:

BubbleSort(arr, n);

break;

case 2:

SelectionSort(arr, n);

break;
case 3:

InsertionSort(arr, n);

break;

default:

printf("Invalid Choice");

break;

printf("Sorted array: \n");

for (int i = 0; i < n; i++) {

printf("%d \n ", arr[i]);

return 0;

OUTPUT

Enter the size of an array: 5

Enter the elements of an array:

Enter 1 for bubble sort, 2 for selection sort, and 3 for insertion sort1

Bubble Sort has been completed.


Sorted array:

TIME COMPLEXITY: O(n^2)+O(n^2)+O(n^2)

SPACE COMPLEXITY: O(1)+O(1)+O(1)

You might also like