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

Name 1

name
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)
13 views7 pages

Name 1

name
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

Name – Koushik Dandapat

Roll – 35000120025
Department – CSE

1.Write down the C code for implementing


bubble sort in an array of n natural numbers.

Definition of bubble sort


Bubble sort is one of the fundamental forms of sorting in
programming. Bubble sort algorithms move through a
sequence of data (typically integers) and rearrange them into
ascending or descending order one number at a time. To do
this, the algorithm compares number X to the adjacent number
Y. If X is higher than Y, the two are swapped and the algorithm
starts over.

Bubble sort algorithm


1. Start at index zero, compare the element with the next
one (a[0] & a[1] (a is the name of the array)), and swap if
a[0] > a[1]. Now compare a[1] & a[2] and swap if a[1] >
a[2]. Repeat this process until the end of the array. After
doing this, the largest element is present at the end. This
whole thing is known as a pass. In the first pass, we
process array elements from [0,n-1].
2. Repeat step one but process array elements [0, n-2]
because the last one, i.e., a[n-1], is present at its correct
position. After this step, the largest two elements are
present at the end.
3. Repeat this process n-1 times.

Code –

#include<stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped = false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
}
}
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");
for(int i = 0 ; i < n ; i++){
printf("%d ", arr[i]);
}
return 0;
}
Flowchart -
2. Write down the C code for implementing insertion
sort in an array of n natural numbers.

Definition - Insertion sort is a simple sorting algorithm that works


similar to the way you sort playing cards in your hands. The array is
virtually split into a sorted and an unsorted part. Values from the
unsorted part are picked and placed at the correct position in the sorted
part.

Algorithm
The simple steps of achieving the insertion sort are listed as follows -

Step 1 - If the element is the first element, assume that it is already


sorted. Return 1.

Step2 - Pick the next element, and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current
element, then move to the next element. Else, shift greater elements in
the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.

Code –
#include <math.h>
#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 = j - 1;
}
arr[j + 1] = key;
}
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

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

return 0;
}
Flowchart -

You might also like