Documente Academic
Documente Profesional
Documente Cultură
Date:
Description:
Linear search, also known as sequential search, is a process that checks every element in the
list sequentially until the desired element is found. The computational complexity for linear
search is O (n), making it generally much less efficient than binary search (O (log n)). But
when list items can be arranged in order from greatest to least and the probabilities appear as
geometric distribution (f (x) = (1-p) x-1p, x=1, 2), then linear search can have the potential to
be notably faster than binary search.
Algorithm:
Linear Search (Array A, Value x)
Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit
Program:
/*linear search in c*/
#include <stdio.h>
using namespace std;
int main()
{
int array[100], search, c, n;
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)
{
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:
When the above c program is compile and executed, it will produce the following result
Best case:
In the best possible case,
The element being searched may be found at the first position.
In this case, the search terminates in success with just one comparison.
Thus in best case, linear search algorithm takes O(1) operations.
Worst Case:
In the worst possible case,
The element being searched may be present at the last position or not present in the array at
all.
In the former case, the search terminates in success with n comparisons.
In the latter case, the search terminates in failure with n comparisons.
Thus in worst case, linear search algorithm takes O(n) operations.
Time Complexity of Linear Search Algorithm is O(n).
Assignment:
Date:
Description:
A binary search, also known as a half-interval search, is an algorithm used in computer
science to locate a specified value (key) within an array. For the search to be binary, the array
must be sorted in either ascending or descending order.
Algorithm:
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Output:
When the above c program is compile and executed, it will produce the following result
for i = 1 to n - 1
min = i
for j = i+1 to n
if list[j] < list[min] then
min = j;
end if
end for
if indexMin != i then
swap list[min] and list[i]
end if
end for
Program:
/*Selection sort in c*/
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
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()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
Output:
When the above c program is compile and executed, it will produce the following result
#include <stdio.h>
void printArray(int array[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", array[i]);
}
printf("\n");
}
void insertionSort(int array[], int size)
{
for (int step = 1; step < size; step++)
{
int key = array[step];
int j = step - 1;
while (key < array[j] && j >= 0)
{
// For descending order, change key<array[j] to key>array[j].
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
int main()
{
int data[] = {9, 5, 1, 4, 3};
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
printf("Sorted array in ascending order:\n");
printArray(data, size);
}
Output:
When the above c program is compile and executed, it will produce the following result
Suppose, an array is in ascending order, and you want to sort it in descending order. In this
case, worse case complexity occers.
Each element has to be compared with each of the other elements so, for every nth
element, (n-1) number of comparisons are made.
Thus, the total number of comparisons = n*(n-1) ~ n2
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Output:
When the above c program is compile and executed, it will produce the following result