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

EXP 11-Sorting

Uploaded by

saarthakk.s
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)
20 views8 pages

EXP 11-Sorting

Uploaded by

saarthakk.s
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/ 8

K. J.

Somaiya College of Engineering, Mumbai-77


Somaiya Vidyavihar University
Department of Computer Engineering

Batch: D1 Roll No.: 16010123239

Experiment / assignment / tutorial No. 11

Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

Title: Implementation of sorting Algorithms.

Objective: To Understand and Implement Bubble & Shell Sort

Expected Outcome of Experiment:

CO Outcome

4 Demonstrate sorting and searching methods.

Books/ Journals/ Websites referred:


1. Fundamentals Of Data Structures In C – Ellis Horowitz, Satraj Sahni, Susan
Anderson-Fred
2. An Introduction to data structures with applications – Jean Paul Tremblay,
Paul G. Sorenson
3. Data Structures A Pseudo Approach with C – Richard F. Gilberg & Behrouz A.
Forouzan

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

Abstract: (Define sorting process, state applications of sorting)

Example:
Take any random unsorted sequence of numbers and solve by using the Bubble and
Shell Sort. Clearly showcase the sorted array after every pass.

The above is a pen-paper activity, take a picture of the solution and put it here.

Algorithm for Implementation:

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

Program:
1)Bubble Sort:

#include <stdio.h>

// Function to perform Bubble Sort

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

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

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

// Swap if the element found is greater than the next element

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

int temp = arr[j];

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

arr[j + 1] = temp;

// Function to print the array

void printArray(int arr[], int size) {

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

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

printf("\n");

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

// Main function to test the Bubble Sort

int main() {

int n;

// Take input for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n]; // Declare an array of size n

// Take input for the array elements

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

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

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

printf("Original array: \n");

printArray(arr, n);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

return 0;

2)Shell Sort:

#include <stdio.h>

// Function to perform Shell sort

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

for (int gap = n / 2; gap > 0; gap /= 2) {

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

int temp = arr[i];

int j;

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {

arr[j] = arr[j - gap];

arr[j] = temp;

// Function to print the array

void printArray(int arr[], int size) {

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

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

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

printf("\n");

// Main function to test the Shell sort

int main() {

int n;

// Take input for the number of elements

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n]; // Declare an array of size n

// Take input for the array elements

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

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

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

printf("Original array: \n");

printArray(arr, n);

shellSort(arr, n);

printf("Sorted array: \n");

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

printArray(arr, n);

return 0;

Output screenshots:
1)Bubble Sort:

2)Shell Sort:

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -


K. J. Somaiya College of Engineering, Mumbai-77
Somaiya Vidyavihar University
Department of Computer Engineering

Conclusion:-

Post Lab Questions:

1) Describe how shell sort improves upon bubble sort. What are the main differences
in their approaches?

2) Explain the significance of the gap in shell sort. How does changing the gap
sequence affect the performance of the algorithm?

3) In what scenarios would you choose shell sort over bubble sort? Discuss the types
of datasets where shell sort performs better.

4) Provide examples of real-world applications or scenarios where bubble sort or shell


sort might be utilized, considering their characteristics.

Department of Computer Engineering DS Sem-III – July-Dec 2024 Page -

You might also like