0% found this document useful (0 votes)
24 views9 pages

DAA EXP 1 and 2

DAA two basic experiments 1 and 2

Uploaded by

Shubham Kamat
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)
24 views9 pages

DAA EXP 1 and 2

DAA two basic experiments 1 and 2

Uploaded by

Shubham Kamat
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/ 9

EXPERIMENT NO:01

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)

1. Let min = 1 max and max= n.

2. Guess the average of max and min rounded down so that it is an integer.

3. If you guessed the number, stop. You found it!

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.

6. Go back to step two.

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:

The Time Complexity of the Bubble Sort Algorithm

 Bubble sort employs two loops: an inner loop and an outer loop.

 The inner loop performs O(n) comparisons deterministically.

Worst Case

 In the worst-case scenario, the outer loop runs O(n) times.

 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:

 Best Case Time Complexity of Binary Search: O(1)

 Average Case Time Complexity of Binary Search: O(logN)

 Worst Case Time Complexity of Binary Search: O(logN)

 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.

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.

(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(){

int i, j, count, temp, number[25];


printf("Deepali yadav,20SCSE1010411...!");
printf("How many numbers u are going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);
for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

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()

int array[100], search, c, n;

printf("Deepali yadav,20SCSE1010411...!");

printf("Enter number of elements in array\n");

scanf("%d", &n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter a number to search\n");

scanf("%d", &search);

for (c = 0; c < n; c++)

if (array[c] == search) /* If required element is found */

printf("%d is present at location %d.\n", search, c+1);

break;

}
}

if (c == n)

printf("%d isn't present in the array.\n", search);

return 0;

OUTPUT:

COMPLEXITY:

 Best Case Time Complexity of Linear Search: O(1)


 Average Case Time Complexity of Linear Search: O(N)
 Worst Case Time Complexity of Linear Search: O(N)
 Space Complexity of Linear Search: O(1)
 Number of comparisons in Best Case: 1
 Number of comparisons in Average Case: N/2 + N/(N+1)
 Number of comparisons in Worst Case: N

You might also like