0% found this document useful (0 votes)
6 views4 pages

Assignment7 1102

Uploaded by

pratapshivam007
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)
6 views4 pages

Assignment7 1102

Uploaded by

pratapshivam007
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
You are on page 1/ 4

1. Write a C program to implement Bubble Sort.

ALGORITHM-
Step 1: Start
Step 2: Repeat Step 3 For i = 0 to n-1
Step 3: Repeat for j = 0 to n - i
Step 4: if arr[j] > arr[j + 1]
Swap arr[j] and a[j+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 5: Exit

PROGRAM-
#include <stdio.h>
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]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array using Bubble Sort:\n");
printArray(arr, n);
return 0;
}

OUTPUT-
Enter number of elements: 5
Enter 5 integers:
42513
Sorted array using Bubble Sort:
12345

2. Write a C program to implement Insertion Sort.

ALGORITHM-
Step 1: Start
Step 2: Repeat steps 3 to 6 for k = 1 to n – 1
Step 3: Set temp = arr[k]
Step 4: Set j = k - 1
Step 5: Repeat while temp <= arr[j]
Set arr[j + 1] = arr[j]
Set j = j - 1
[end of inner loop]
Step 6: Set arr[j + 1] = temp
[end of loop]
Step 7: Exit

PROGRAM-
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 0; i <=n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
--j;
}
arr[j+1] = key;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted array using Insertion Sort:\n");
printArray(arr, n);
return 0;
}

OUTPUT-
Enter number of elements: 5
Enter 5 integers:
42513
Sorted array using Insertion Sort:
12345

3. Write a C program to implement Selection Sort.

ALGORITHM-
Step 1: Start
Step 1: Repeat steps 2 and 3 for k = 1
To n-1
Step 2: Call smallest(arr, k, n, pos) from step 4
Step 3: Swap arr[k] with arr[pos]
[end of loop]
Step 4: [initialize] set small = arr[k]
Step 5: [initialize] set pos = k
Step 6: Repeat for j = k+1 to n-1
If small > arr[j]
Set small = arr[j]
Set pos = j
[end of if]
[end of loop]
Step 7: Return pos
Step 8: Exit

PROGRAM-
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int minIndex = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main() {
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
selectionSort(arr, n);
printf("Sorted array using Selection Sort:\n");
printArray(arr, n);
return 0;
}

OUTPUT-
Enter number of elements: 5
Enter 5 integers:
42513
Sorted array using Selection Sort:
12345

You might also like