Professional Documents
Culture Documents
Datastructure Practical - 5
Datastructure Practical - 5
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)
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>
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]);
}
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.
-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.
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