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

Dsa 7 Alt

The document outlines a lab focused on implementing three sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort using C programming. It discusses the algorithms' mechanisms, time complexities, and provides source code for each implementation. The lab emphasizes the importance of understanding sorting techniques and their efficiency in handling various data scenarios.

Uploaded by

Himal Bhattarai
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)
4 views8 pages

Dsa 7 Alt

The document outlines a lab focused on implementing three sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort using C programming. It discusses the algorithms' mechanisms, time complexities, and provides source code for each implementation. The lab emphasizes the importance of understanding sorting techniques and their efficiency in handling various data scenarios.

Uploaded by

Himal Bhattarai
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

TITLE: TO IMPLEMENT VARIOUS SORTING

ALGORITHMS

INTRODUCTION
This lab focused on the implementation of three fundamental sorting algorithms, Bubble Sort,
Insertion Sort, and Selection Sort, using the C programming language. Sorting plays a crucial
role in computer science as it prepares data for efficient processing, searching, and
presentation. These algorithms were selected due to their simplicity and educational value in
understanding how comparison-based sorting works. The aim of the lab was to implement
each algorithm individually, observe their behavior on different inputs, and analyze their time
complexities in various scenarios.
THEORY
The process of arranging elements in a specific order, commonly numerical or
lexicographical, is called sorting. It is a basic operation in many applications and serves as
the basis for more complex algorithms. In this lab, three well-known sorting methods were
studied and implemented:
I. Bubble Sort
Bubble Sort compares adjacent elements and swaps them if they are out of order. This
process is repeated until the entire list is sorted.
Time Complexity:
 Best Case: O(n)
 Average Case: O(n²)
 Worst Case: O(n²)
II. Insertion Sort
Insertion Sort builds a sorted section of the array by placing each new element in its correct
position.
Time Complexity:
 Best Case: O(n)
 Average Case: O(n²)
 Worst Case: O(n²)

III. Selection Sort


Selection Sort repeatedly selects the smallest element from the unsorted portion and swaps it
with the first unsorted item.

1
Time Complexity:
 Best Case: O(n²)
 Average Case: O(n²)
 Worst Case: O(n²)

Algorithm for Bubble sort:


i. Loop from i = 0 to n-2
ii. Set flag x = 0
iii. Loop from j = 0 to n-i-2
a. If a[j] > a[j+1], swap and set x = 1
iv. If x == 0, break (already sorted)
Algorithm for Insertion sort:
i. For i = 1 to n-1
ii. val = a[i], hole = i
iii. While hole > 0 and a[hole-1] > val
a. Shift a[hole-1] to a[hole], decrement hole
iv. Insert val at a[hole]
Algorithm for Selection sort:
i. For i = 0 to n-1
ii. Set p = i
iii. For j = i+1 to n-1
a. If a[j] < a[p], update p
iv. Swap a[i] with a[p]

TASK PERFORMED
I. Write a program to implement bubble sort algorithm.
II. Write a program to implement insertion sort algorithm.
III. Write a program to implement selection sort algorithm

2
SOURCE CODE
I.
#include<stdio.h>
void swap(int *a,int *b){
int temp=*a;
*a=*b;
*b=temp;
}
void prnt(int a[],int n){
for(int i=0;i<n;i++){
printf("%d\t",a[i]);
}
}
void bubbleSort(int a[],int n){
int x,i,j;
for(i=0;i<n-1;i++){
x=0;
for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
swap(&a[j],&a[j+1]);
x++;
}
}
if(x==0){
printf("\nList is sorted\n");
prnt(a,n);
break;
}
printf("\nPass %d:",i+1);
prnt(a,n);
}
}
int main(){
int a[100],n,i,j;
printf("Enter the number of data, n: ");
scanf("%d",&n);
printf("Enter the data elements:\n");
for(i=0;i<n;i++){
printf("a[%d]:",i);
scanf("%d",&a[i]);
}
bubbleSort(a,n);
}

3
II.
#include<stdio.h>
void prnt(int a[],int n){
for(int i=0;i<n;i++){
printf("%d\t",a[i]);
}
}
void insrtSort(int a[],int n){
int i,val,hole;
for(i=1;i<n;i++){
val=a[i];
hole=i;
while(hole>0 && a[hole-1]>val){
a[hole]=a[hole-1];
hole=hole-1;
}
a[hole]=val;
printf("\nPass %d:",i);
prnt(a,n);
}
}
int main(){
int a[100],n,i,j;
printf("Enter the number of data, n: ");
scanf("%d",&n);
printf("Enter the data elements:\n");
for(i=0;i<n;i++){
printf("a[%d]:",i);
scanf("%d",&a[i]);
}
insrtSort(a,n);
}

III.
#include<stdio.h>
void swap(int *a,int *b){
int temp=*a;
*a=*b;
*b=temp;
}
void prnt(int a[],int n){
for(int i=0;i<n;i++){
printf("%d\t",a[i]);

4
}
}
void selSort(int a[],int n){
int i,j,p,less;
for(i=0;i<n;i++){
less=a[i];
p=i;
for(j=i+1;j<n;j++){
if(a[j]<less){
less=a[j];
p=j;
}
}
swap(&a[i],&a[p]);
printf("\nPass %d:",i+1);
prnt(a,n);
}
}
int main(){
int a[100],n,i,j;
printf("Enter the number of data, n: ");
scanf("%d",&n);
printf("Enter the data elements:\n");
for(i=0;i<n;i++){
printf("a[%d]:",i);
scanf("%d",&a[i]);
}
selSort(a,n);
}

5
OUTPUT
I. Bubble Sort

Entering number of items

Data input

Sorting passes

Already sorted list (best case)

II. Insertion Sort

Entering number of items

Data input

6
Sorting Passes

III. Selection Sort

Entering number of items

Data input

Sorting Passes

7
DISCUSSION
The lab demonstrated how different sorting techniques work under the hood and how they
behave with varying input types. Bubble Sort was observed to perform optimally when the
list was already sorted, as indicated by the use of a flag variable that prevented unnecessary
passes. It showed the concept of repeated adjacent comparison and swap operations, making
it intuitive but inefficient for large datasets.
Insertion Sort efficiently built a sorted array by inserting elements at their appropriate
locations, with fewer comparisons and shifts when the data was nearly sorted, making it
suitable for small or partially sorted data.
Selection Sort, on the other hand, consistently found the minimum element in the remaining
unsorted part, irrespective of the input condition, thus having a uniform performance but
often resulting in more swaps compared to Insertion Sort. Its simplicity makes it useful when
memory writes are more expensive than reads.
Through step-by-step observation and dry runs, we also better understood the time
complexity of each algorithm in the best, average, and worst-case scenarios. This experience
deepened our understanding of algorithm efficiency, helping us recognize the trade-offs
between readability, performance, and implementation complexity. Moreover, comparing all
three algorithms side by side allowed us to appreciate why more advanced algorithms like
Merge Sort, Quick Sort, or Heap Sort are preferred in real-world applications.
Overall, the lab not only reinforced our coding skills in C but also emphasized the importance
of choosing the right algorithm depending on the size and nature of the dataset.

CONCLUSION
This lab session enabled us to understand and implement basic sorting algorithms in C. By
observing each step of the sorting process, we gained insight into how elements are
compared, shifted, and swapped to achieve a sorted array. These fundamental concepts are
crucial stepping stones to grasp more complex and efficient algorithms. Overall, the lab
served as a practical exercise in algorithm analysis and C programming.

You might also like