Array
Array
Home
Searching in Arrays
Average: O(n)
Best: O(1)
Worst: O(n)
Average: O(n)
Best: O(1)
Worst: O(n
Binary Search
The linear search approach has one disadvantage. Finding elements in a large array will
be time
consuming. As the array grows, the time will increase linearly. A binary search can be
used as a
solution to this problem in some cases.
The principle of binary search is how we find a page in book. We open the book at a
random page in the middle and based on that page we narrow our search to the left or
right of the book. Indeed, this only is possible if the page numbers are in order.
Binary search
Hence, the prerequisite of performing a binary search is that the array must be sorted.
That is why this works only in cases where keeping a sorted copy of the array is
possible.
The search starts by accessing the middle of the array. If the element is less than this
element, it
starts its search from this element to the left of the array. If the element is larger more
than this
element, it starts its search from this element to the right of the array. This process is
repeated unless a the middle element is equal to the number we are searching.
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Best use case
This search is best used when the list of elements is already sorted (not always feasible,
especially when new elements are frequently being inserted). The list to be searched can
be very large without much decrease in searching time, due to the logarithmic time
complexity of the algorithm.
Time Complexity
Sorting in Arrays
Sorting an array means to arrange the elements in the array in a certain order. Various
algorithms
have been designed that sort the array using different methods. Some of these sorts are
more useful than the others in certain situations.
Terminologies
Internal/External Sorting
Internal sorting means that all the data that is to be sorted is stored in memory while
sorting is in progress.
External sorting means that the data is stored outside memory (like on disk) and only
loaded into memory in small chunks. External sorting is usually applied in cases when
data can’t fit into memory entirely, effectively allowing to sort data that does not fit in
the memory.
Stability of Sort
A sorting algorithm is said to be stable if two objects with equal keys appear in the
same order in the sorted output as they appear in the unsorted input.
A sorting algorithm is said to be unstable if there are two or more objects with equal
keys which don’t appear in same order before and after sorting.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent
elements if they are in wrong order. The pass through the list is repeated until the list is
sorted.
#include <stdio.h>
int main()
{
int arr[100], n, i, j, temp;
return 0;
}
Properties
Insertion Sort
In insertion sort, every iteration moves an element from unsorted portion to sorted
portion until all theelements are sorted in the list. An analogy of insertion sort is the
sorting of a deck of cards with our hands. We select one card from the unsorted deck
and put it in the right order in our hands, effectively sorting the whole deck.
Steps
Steps
1. Assume that first element in the list is in its sorted portion of the list and remaining all
elements are in unsorted portion.
2. Take the first element from the unsorted list and insert that element into the sorted list
in order
specified (ascending or descending).
3. Repeat the above process until all the elements from the unsorted list are moved into
the sorted
list.
Code for Insertion Sort
#include<stdio.h>
int main()
{
int data[100],n,temp,i,j;
printf("Enter number of elements to be sorted:");
scanf("%d",&n);
printf("Enter elements: ");
for(i = 0; i < n; i++)
scanf("%d",&data[i]);
for(i = 1; i < n; i++)
{
temp = data[i];
j = i - 1;
while(temp < data[j] && j>=0)
{
data[j + 1] = data[j];
j = j - 1;
}
data[j + 1]=temp;
}
printf("Sorted array: ");
for(i = 0; i < n; i++)
printf("%d ",data[i]);
return 0;
}
inary Search
Properties
Properties
Properties
int main()
{
int array[100], n, pos, temp, i, j;
printf("Enter number of elements\n");
scanf("%d", &n);
return 0;
}
Properties