BCSL 045
BCSL 045
BCSL 045
1
Answer.
The selection sort algorithm sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it at the beginning. The
algorithm maintains two subarrays in a given array.
1)The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order)
from the unsorted subarray is picked and moved to the sorted subarray.
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
27,15,12,14,17,9,11,25,5,3
#include <stdio.h>
#include<conio.h>
Question.2
Answer:
QuickSort
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as
pivot and partitions the given array around the picked pivot. There are many different
versions of quickSort that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all
smaller elements (smaller than x) before x, and put all greater elements (greater than
x) after x. All this should be done in linear time.
at right place */
Partition Algorithm
There can be many ways to do partition, following pseudo code adopts the method
given in CLRS book. The logic is simple, we start from the leftmost element and keep
track of index of smaller (or equal to) elements as i. While traversing, if we find a
smaller element, we swap current element with arr[i]. Otherwise we ignore current
element.
/* low --> Starting index, high --> Ending index */
at right place */
of pivot */
pivot = arr[high];
// equal to pivot
return (i + 1)
}
Illustration of partition() :
arr[] = {70,90,40,30,15,25,20,10,67,5}
Indexes: 0 1 2 3 4 5 6 7 8 9
/* C implementation QuickSort */
#include<stdio.h>
Question 3.
Answer.
Explanation Of Program :
Firstly find the length of the string using library function strlen().
j = strlen(str)-1;
1 j = strlen(str)-1;
1 j = strlen(str)-1;
2 = strlen("Pritesh") - 1
3 =7-1
4 =6
As we know String is charracter array and Character array have character range between 0 to
length-1. Thus we have position of last character in variable ‘j’.Current Values of ‘i’ and ‘j’ are
–
i = 0;
j = 6;
1 i = 0;
2 j = 6;
‘i’ positioned on first character and ‘j’ positioned on last character. Now we are swapping
characters at position ‘i’ and ‘j’. After interchanging characters we are incrementing value of ‘i’
and decrementing value of ‘j’.
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
1 while (i < j) {
2 temp = str[i];
3 str[i] = str[j];
4 str[j] = temp;
5 i++;
6 j--;
7 }
Program
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp,ecount=0,ccount=0,lcount=0;
int i, j = 0;
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
ecount++; //calculating total number of exchange operation
ccount++; //calculating total number of comparison operation
lcount++; //calculating total number of loop execution
}
Question 4.
Answer.
Binary Search
Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].
A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another
approach to perform the same task is using Binary Search.
Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an
interval covering the whole array. If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check
until the value is found or the interval is empty.
The idea of binary search is to use the information that the array is sorted and reduce the time
complexity to O(Log n).
int main(void)
{
int arr[] = {4,7,12,24,29,30,40,50,55,60};
int n = sizeof(arr)/ sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("Element is not present"
" in array")
: printf("Element is present at "
"index %d", result);
return 0;