DAA EXP 1 and 2
DAA EXP 1 and 2
AIM: To write a program to sort given set of numbers in ascending/descending order using
Bubble sort and also search a number using binary search.
ALGORITHM: (BUBBLE SORT)
1. Starting with the first element(index = 0), compare the current element with the next
element of the array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element. Repeat
Step 1.
(BINARY SEARCH)
2. Guess the average of max and min rounded down so that it is an integer.
4. If the guess was too low, set min, to be one larger than the guess.
5. If the guess was too high, set max, to be one smaller than the guess.
FLOWCHART:
SOURCE CODE:
(BUBBLE SORT)
1. #include <stdio.h>
2. #define MAXSIZE 10
3.
4. void main()
5. {
6. int array[MAXSIZE];
7. int i, j, num, temp;
8. printf(“Deepali yadav,20SCSE1010411….!”);
9. printf("Enter the value of num \n");
10. scanf("%d", &num);
11. printf("Enter the elements one by one \n");
12. for (i = 0; i < num; i++)
13. {
14. scanf("%d", &array[i]);
15. }
16. printf("Input array is \n");
17. for (i = 0; i < num; i++)
18. {
19. printf("%d\n", array[i]);
20. }
21. /* Bubble sorting begins */
22. for (i = 0; i < num; i++)
23. {
24. for (j = 0; j < (num - i - 1); j++)
25. {
26. if (array[j] > array[j + 1])
27. {
28. temp = array[j];
29. array[j] = array[j + 1];
30. array[j + 1] = temp;
31. }
32. }
33. }
34. printf("Sorted array is...\n");
35. for (i = 0; i < num; i++)
36. {
37. printf("%d\n", array[i]);
38. }
39. }
OUTPUT:
COMPLEXITY:
Bubble sort employs two loops: an inner loop and an outer loop.
Worst Case
As a result, the worst-case time complexity of bubble sort is O(n x n) = O(n x n) (n2).
Best Case
In the best-case scenario, the array is already sorted, but just in case, bubble sort performs
O(n) comparisons.
As a result, the time complexity of bubble sort in the best-case scenario is O(n).
Average Case
Bubble sort may require (n/2) passes and O(n) comparisons for each pass in the average
case.
As a result, the average case time complexity of bubble sort is O(n/2 x n) = O(n/2 x n) =
O(n/2 x n) = O(n/2 x n) = O (n2).
(BINARY SEARCH)
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Deepali yadav,20SCSE1010411....!");
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}
OUTPUT:
COMPLEXITY:
Space Complexity of Binary Search: O(1) for iterative, O(logN) for recursive
EXPERIMENT NO:02
AIM: To write a program to sort given set of numbers in ascending/descending order using
Insertion sort and also search a number using linear search.
ALGORITHM: (INSERTION SORT)
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.
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.
(LINEAR SEARCH)
Step 1 - First, we have to traverse the array elements using a for loop.
Step 2 - In each iteration of for loop, compare the search element with the current array
element, and –
i) If the element matches, then return the index of the corresponding array element.
ii)If the element does not match, then move to the next element.
Step 3 - If there is no match or the search element is not present in the given array, return -1.
FLOWCHART:
SOURCE CODE:
(INSERTION SORT)
#include<stdio.h>
int main(){
return 0;
}
OUTPUT:
COMPLEXITY:
Worst case time complexity: Θ(n^2)
Average case time complexity: Θ(n^2)
Best case time complexity: Θ(n)
Space complexity: Θ(1)
(LINEAR SEARCH)
#include <stdio.h>
int main()
printf("Deepali yadav,20SCSE1010411...!");
scanf("%d", &n);
scanf("%d", &array[c]);
scanf("%d", &search);
break;
}
}
if (c == n)
return 0;
OUTPUT:
COMPLEXITY: