Data Structure

Searching : searching is one of the basic operation of data structure.

Searching means finding location of given  key element  in list,and return its position if search is successful.

There are two types of searching

Linear search

Binary search

A linear search is the basic and simple search algorithm. A linear search searches an element or value from an array till the desired element or value is not found and it searches in a sequence order. It compares the element with all the other elements given in the list and if the element is matched it returns the value index else it return -1. Linear Search is applied on the unsorted or un ordered list when there are fewer elements in a list.

Binary Search

Binary Search is applied on the sorted array or list. In binary search, we first compare the value with the elements in the middle position of the array. If the value is matched, then we return the value. If the value is less than the middle element, then it must lie in the lower half of the array and if it's greater than the element then it must lie in the upper half of the array. We repeat this procedure on the lower (or upper) half of the array. Binary Search is useful when there are large numbers of elements in an array.

 Sorting

Arrangement of elements of list in particular logical order  i.e.ascending or descending  is sorting.

InternalExternal
(In memory)Appropriate for secondary storage
quick sort 
heap sortmergesort
bubble sortradix sort
insertion sort
selection sort 
shell sort 

Bubble sort 

The bubble sort makes multiple passes through a list. It compares adjacent items and exchanges those that are out of order. Each pass through the list places the next largest value in its proper place. In essence, each item “bubbles” up to the location where it belongs.

Fig 1. shows the first pass of a bubble sort. The shaded items are being compared to see if they are out of order. If there are n items in the list, then there are n1 pairs of items that need to be compared on the first pass. It is important to note that once the largest value in the list is part of a pair, it will continually be moved along until the pass is complete.

bubble sort example


At the start of the second pass, the largest value is now in place. There are n1 items left to sort, meaning that there will be  pairs. Since each pass places the next largest value in place, the total number of passes necessary will be  After completing the n1 passes, the smallest item must be in the correct position with no further processing required. . It takes the list as a parameter, and modifies it by exchanging items as necessary.

Selection sort 

Selection sort is a simple sorting algorithm. This sorting algorithm is a in-place comparison based algorithm in which the list is divided into two parts, sorted part at left end and unsorted part at right end. Initially sorted part is empty and unsorted part is entire list.

Smallest element is selected from the unsorted array and swapped with the leftmost element and that element becomes part of sorted array. This process continues moving unsorted array boundary by one element to the right.

This algorithm is not suitable for large data sets as its average and worst case complexity are of O(n2) where n are no. of items.

selection sort

Worst Case Time Complexity : O(n2)
Best Case Time Complexity : O(n2)
Average Time Complexity : O(n2)
Space Complexity : O(1)


Insert sort 

The insertion sort, although still O(n2), works in a slightly different way. It always maintains a sorted sub list in the lower positions of the list. Each new item is then “inserted” back into the previous sublist such that the sorted sublist is one item larger. Figure  shows the insertion sorting process. The shaded items represent the ordered sublists as the algorithm makes each pass.

insert sort


AlgorithmTime Complexity

BestAverage



Insertion SortO(n)O(n^2)

Radix sort 

The Radix Sort Algorithm
1) Do following for each digit i where i varies from least significant digit to the most significant digit.
………….a) Sort input array using counting sort (or any stable sort) according to the i’th digit.

Example:
Original, unsorted list:

170, 45, 75, 90, 802, 24, 2, 66

Sorting by least significant digit (1s place) gives: [*Notice that we keep 802 before 2, because 802 occurred before 2 in the original list, and similarly for pairs 170 & 90 and 45 & 75.]

170, 90, 802, 2, 24, 45, 75, 66

Sorting by next digit (10s place) gives: [*Notice that 802 again comes before 2 as 802 comes before 2 in the previous list.]

802, 2, 24, 45, 66, 170, 75, 90

Sorting by most significant digit (100s place) gives:

2, 24, 45, 66, 75, 90, 170, 802
Time complexity
O(nk)

Return to top