0% found this document useful (0 votes)
5 views7 pages

DS - 4 827

The document contains C code implementations for three sorting algorithms: optimized bubble sort, insertion sort, and selection sort. Each algorithm includes a main function that prompts the user for the number of elements and the elements themselves, sorts the array, and then displays the sorted array. The code is structured with comments indicating user input and output sections.

Uploaded by

ansh2004jagdhari
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)
5 views7 pages

DS - 4 827

The document contains C code implementations for three sorting algorithms: optimized bubble sort, insertion sort, and selection sort. Each algorithm includes a main function that prompts the user for the number of elements and the elements themselves, sorts the array, and then displays the sorted array. The code is structured with comments indicating user input and output sections.

Uploaded by

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

Practical Assignment 4

NAME: VIDIT MANIK KHUNE

PRN: 20240802827

DIV: F

1.Write C code to implement optimized bubble sort.


#include <stdio.h>

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

int i, j, temp, swapped;

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

swapped = 0;

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

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

// Swap elements

temp = arr[j];

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

arr[j + 1] = temp;

swapped = 1;

// If no swaps, array is already sorted

if(swapped == 0)

break;

}
}

int main() {

int arr[100], n;

// ✅ Asking user for number of elements

printf("Enter number of elements: ");

scanf("%d", &n);

// ✅ Asking user to input array elements

printf("Enter %d elements:\n", n);

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

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

bubbleSort(arr, n);

// ✅ Display sorted array

printf("Sorted array (Optimized Bubble Sort):\n");

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

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

return 0;

OUTPUT:
2. Write C code to implement insertion sort.
#include <stdio.h>

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--;

}
arr[j + 1] = key;

int main() {

int arr[100], n;

// ✅ Input size

printf("Enter number of elements: ");

scanf("%d", &n);

// ✅ Input elements

printf("Enter %d elements:\n", n);

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

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

insertionSort(arr, n);

// ✅ Output sorted array

printf("Sorted array (Insertion Sort):\n");

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

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

return 0;

OUTPUT:
3. Write C code to implement selection sort.
#include <stdio.h>

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

int i, j, minIndex, temp;

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

minIndex = i;

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

if(arr[j] < arr[minIndex])

minIndex = j;

// Swap smallest found with current element

if(minIndex != i) {

temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;
}

int main() {

int arr[100], n;

// ✅ Get array size

printf("Enter number of elements: ");

scanf("%d", &n);

// ✅ Get array elements

printf("Enter %d elements:\n", n);

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

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

selectionSort(arr, n);

// ✅ Output result

printf("Sorted array (Selection Sort):\n");

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

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

return 0;
} OUTPUT:

You might also like