You are on page 1of 21

DATA STRUCTURES

Module VI
Mrs.D.Madhina Banu
MODULE VI SORTING AND SEARCHING 07

Sorting – Insertion Sort – Selection Sort – Merging – Merge Sort – Radix Sort –
Searching and Data modification – Hashing
Sorting
Sorting is any process of arranging items systematically, and has two
common, yet distinct meanings: ordering: arranging items in a sequence ordered
by some criterion; categorizing: grouping items with similar properties.
Selection Sort

•Idea:
–Find the smallest element in the array
–Exchange it with the element in the first position
–Find the second smallest element and exchange it with the element in
the second position
–Continue until the array is sorted
•Disadvantage:
–Running time depends only slightly on the amount of order in the file
Selection sort
Merge Sort
Merge Sort is a Divide and Conquer algorithm. It divides the input array into two halves,
calls itself for the two halves, and then merges the two sorted halves. The merge()
function is used for merging two halves.
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller
sorted lists keeping the new list sorted too.
Step 1 − if it is only one element in the list it is already sorted, return.

Step 2 − divide the list recursively into two halves until it can no more be
divided.

Step 3 − merge the smaller lists into new list in sorted order.
Radix sort

Radix sort is the linear sorting algorithm that is used for integers.

In Radix sort, there is digit by digit sorting is performed that is started from the least
significant digit to the most significant digit.

The process of radix sort works similar to the sorting of students names, according to the alphabetical
order.

In this case, there are 26 radix formed due to the 26 alphabets in English. In the first pass, the names of
students are grouped according to the ascending order of the first letter of their names. After that, in the
second pass, their names are grouped according to the ascending order of the second letter of their
name. And the process continues until we find the sorted list.
Algorithm
1. radixSort(arr)
2. max = largest element in the given array
3. d = number of digits in the largest element (or, max)
4. Now, create d buckets of size 0 - 9
5. for i -> 0 to d
6. sort the array elements using counting sort (or any stable sort) according to the digits at
7. the ith place
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 the most significant digit (100s place) gives:

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


Searching

Searching is an operation or a technique that helps finds the place of a given element or value in the list.

Any search is said to be successful or unsuccessful depending upon whether the element that is being searched is found or
not.

Some of the standard searching technique that is being followed in the data structure is listed below:

Linear Search or Sequential Search


Binary Search
Linear search
This is the simplest method for searching.

In this technique of searching, the element to be found in searching the elements to be found is searched sequentially in the
list.

This method can be performed on a sorted or an unsorted list (usually arrays).

In case of a sorted list searching starts from 0th element and continues until the element is found from the list or the element
whose value is greater than (assuming the list is sorted in ascending order), the value being searched is reached.
Binary Search
Binary search is a very fast and efficient searching technique.

It requires the list to be in sorted order.

To search an element can compare it with the present element at the center of the list.

If it matches, then the search is successful otherwise the list is divided into two halves: one from the 0 th element to the
middle element which is the center element (first half) another from the center element to the last element (which is the 2 nd
half) where all values are greater than the center element.
Hashing
Hashing in the data structure is a technique of mapping a large chunk of data into small tables using a hashing
function.

It is also known as the message digest function.

It uses hash tables to store the data in an array format.

Each value in the array has assigned a unique index number.


The hash function in a data structure maps arbitrary size of data to fixed-sized data. It returns the

following values: a small integer value (also known as hash value), hash codes, and hash sums.

hash = hashfunc(key)

index = hash % array_size


7,8,33,66,90,32,45,67,54,87
90 90
0 90,

2 32

3 33

4 54

5 45

6 66

7 7,67,87 7 67 87

8 8

You might also like