You are on page 1of 12

20SOECE13007_DURAGIYA HITESH V.

DATA STRUCTURE(CE317)

Tutorial – 5

R.K. UNIVERSITY 1
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :

R.K. UNIVERSITY 2
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

1. Apply selection and bubble sort method to arrange the given below data in ascending
order 12, 54,23,6,78,89,5,7,34

#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;
for (i = 0; i < n-1; i++)
{
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
swap(&arr[j], &arr[j+1]);
}
}
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{

R.K. UNIVERSITY 3
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

int arr[] = {12,54,23,6,78,89,5,7,34};


int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("after bubbleSort \n");
printArray(arr, n);
selectionSort(arr,n);
printf("after selectionSort \n");
printArray(arr, n);
return 0;
}

R.K. UNIVERSITY 4
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :

R.K. UNIVERSITY 5
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

2. Apply insertion and shell sort method to arrange the given below data in ascending
order 11,55,22,6,78,89,55,77,34

#include <stdio.h>
#include <stdbool.h>
#include <math.h>

void swap(int *xp, int *yp)


{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void insertionSort(int array[], int n)
{
int i, key, j;
for (i = 1; i < n; i++) {
key = array[i];
j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = key;
}
}
void shellSort(int array[], int n) {
for (int interval = n / 2; interval > 0; interval /= 2) {
for (int i = interval; i < n; i += 1) {
int temp = array[i];
int j;
for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {
array[j] = array[j - interval];
}
array[j] = temp;
}
}
}
void printArray(int array[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", array[i]);

R.K. UNIVERSITY 6
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

printf("\n");
}
int main()
{
int array[] = {11,55,22,6,78,89,55,77,34};
int n = sizeof(array)/sizeof(array[0]);
insertionSort(array, n);
printf("after insertionSort\n");
printArray(array, n);
shellSort(array,n);
printf("after selectionSort \n");
printArray(array, n);
return 0;
}

R.K. UNIVERSITY 7
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

Output :

R.K. UNIVERSITY 8
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

3. Apply radix sort method to arrange the given below data in ascending order
12,54,23,6,78,89,5,7,34

#include <stdio.h>
int largest(int a[]);
void radix_sort(int a[]);
void main()
{
int i;
int a[10]={12,54,23,6,78,89,5,7,34,11};
radix_sort(a);
printf("\n The sorted array is: \n");
for(i=0;i<10;i++)
printf(" %d\t", a[i]);
}

int largest(int a[])


{
int larger=a[0], i;
for(i=1;i<10;i++)
{
if(a[i]>larger)
larger = a[i];
}
return larger;
}
void radix_sort(int a[])
{
int bucket[10][10], bucket_count[10];
int i, j, k, remainder, NOP=0, divisor=1, larger, pass;
larger = largest(a);
while(larger>0)
{
NOP++;
larger/=10;
}
for(pass=0;pass<NOP;pass++)
{
for(i=0;i<10;i++)
bucket_count[i]=0;
for(i=0;i<10;i++)
{
remainder = (a[i]/divisor)%10;

R.K. UNIVERSITY 9
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

bucket[remainder][bucket_count[remainder]] = a[i];
bucket_count[remainder] += 1;
}
i=0;
for(k=0;k<10;k++)
{
for(j=0;j<bucket_count[k];j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}

R.K. UNIVERSITY 10
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

4. Differentiate Linear and binary search on the following array based on its
implementation 5 ,6,7,10,15,25,56,67,89,90

 Linear Search:

Find 25

0 1 2 3 4 5 6 7 8

5 6 7 10 25 56 67 89 90

A linear search scans one item at a time, without jumping to any item.

-The worst case complexity is O(n), sometimes known an O(n) search

-Time taken to search elements keep increasing as the number of elements are
increased.

 Binary search :

Find 25

0 1 2 3 4 5 6 7 8

5 6 7 10 25 56 67 89 90

-A binary search however, cut down your search to half as soon as you find middle of a
sorted list.
R.K. UNIVERSITY 11
20SOECE13007_DURAGIYA HITESH V. DATA STRUCTURE(CE317)

-The middle element is looked to check if it is greater than or less than the value to be
searched.

-Accordingly, search is done to either half of the given list.

 Important Differences

-Input data needs to be sorted in Binary Search and not in Linear Search

-Linear search does the sequential access whereas Binary search access data randomly.

-Time complexity of linear search -O(n) , Binary search has time complexity O(log n).

-Linear search performs equality comparisons and Binary search performs ordering
comparisons

R.K. UNIVERSITY 12

You might also like