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

DSL Experiment No 8 STD

The document describes an experiment implementing several sorting algorithms in C code: selection sort, bubble sort, insertion sort, and quick sort. Code snippets are provided for each algorithm's implementation. The main function tests each algorithm on sample integer arrays and prints the sorted output. The conclusion states the experiment implemented and tested these common sorting methods.

Uploaded by

Lakshit Patil
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)
47 views7 pages

DSL Experiment No 8 STD

The document describes an experiment implementing several sorting algorithms in C code: selection sort, bubble sort, insertion sort, and quick sort. Code snippets are provided for each algorithm's implementation. The main function tests each algorithm on sample integer arrays and prints the sorted output. The conclusion states the experiment implemented and tested these common sorting methods.

Uploaded by

Lakshit Patil
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/ 7

Department of Information Technology

Academic Year: 2020-2021 Name of Student:Lakshit patil Student ID:20104043


Semester: IV
Class / Branch: SE IT
Subject: Data Structure Lab
Name of Instructor:

Experiment No. 08
Aim: Implementation of Selection sort, Bubble sort, Insertion sort, Quick sort.
Code:
// C program for implementation of selection sort
#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;

// Swap the found minimum element with the first element


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

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:

Code:
// C program for implementation of Bubble sort
#include <stdio.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort


void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)

// Last i elements are already in place


for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}

/* Function to print an array */


void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
Code:
// C program for insertion sort
#include <math.h>
#include <stdio.h>

/* Function to sort an array using insertion sort*/


void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1], that are


greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

// A utility function to print an array of size n


void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

/* Driver program to test insertion sort */


int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);
printArray(arr, n);

return 0;
}
Output:

Quick Sort:
// Quick sort in C

#include <stdio.h>

// Function to swap position of elements


void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}

// Function to partition the array on the basis of pivot element


int partition(int array[], int low, int high) {

// Select the pivot element


int pivot = array[high];
int i = (low - 1);

// Put the elements smaller than pivot on the left


// and greater than pivot on the right of pivot
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}

swap(&array[i + 1], &array[high]);


return (i + 1);
}

void quickSort(int array[], int low, int high) {


if (low < high) {

// Select pivot position and put all the elements smaller


// than pivot on left and greater than pivot on right
int pi = partition(array, low, high);

// Sort the elements on the left of pivot


quickSort(array, low, pi - 1);

// Sort the elements on the right of pivot


quickSort(array, pi + 1, high);
}
}

// Function to print eklements of an array


void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}

// Driver code
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}
Output:

Conclusion: -
Thus in this experiment we implemented Selection sort, Bubble sort, Insertion sort, Quick sort.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy