0% found this document useful (0 votes)
39 views12 pages

Searching and Sorting

The document discusses various sorting and searching techniques including bubble sort, merge sort, selection sort, heap sort, sequential search, and binary search. It provides details on the algorithms and time complexities of each technique.

Uploaded by

abc def
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Searching and Sorting

The document discusses various sorting and searching techniques including bubble sort, merge sort, selection sort, heap sort, sequential search, and binary search. It provides details on the algorithms and time complexities of each technique.

Uploaded by

abc def
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Searching and Sorting Techniques

Data Structures and Algorithms


Introduction
• Sorting is one of the most fundamental algorithmic problems. Sorting
is an operation that segregates items into groups according to
specified criterion and it can be applied to either numerical or
alphanumerical data.
• Searching is the operation which finds the location in memory of
some given ITEM, then the process is success. Otherwise it sends
message about the non availability of the particular ITEM. The search
is said to successful or unsuccessful according to whether the ITEM is
present or not.
Sorting
• Sorting is the process of arranging a collection of items in some
specified order.
• Sorting sequence may be ascending or descending numerical, lexical
ordering, or date.
• Sorting is the subject of a great deal of study since it is a common
operation which can consume a lot of computer time.
• Here we are going to discuss bubble, merge, selection and heap
sorting techniques.
Bubble sort
• Bubble sort is a straightforward and simplistic method of sorting data
that is used very commonly.
• The algorithm starts at the beginning of the data set. It compares the
first two elements, and if the first is greater than the second, then it
swaps them.
• It continues doing this for each pair of adjacent elements to the end
of the data set. It then starts again with the first two elements,
repeating until no swaps have occurred on the last pass.
• This algorithm is highly inefficient, and is rarely used except as a
simplistic example.
• Bubble sort average case and worst case are both O(n²).
Merge sort
• Divide-and conquer is a general algorithm design paradigm:
• Divide: divide the input data S in two disjoint subsets S1and S2
• Recur: solve the sub problems associated with S1 and S2
• Conquer: combine the solutions for S1 and S2 into a solution for S
• The base case for the recursion is sub problems of size 0 or 1
• Merge-sort on an input sequence S with n elements consists of three
steps:
• Divide: partition S into two sequences S1 and S2 of about n/2 elements each
• Recur: recursively sort S1 and S2
• Conquer: merge S1 and S2 into a unique sorted sequence.
Selection sort
• Selection sort is one of the sorting techniques that is typically used for
sequencing small lists.
• Selection sorts perform numerous comparisons, but fewer data
movements than other methods.
• Algorithm for selection sort:
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for the remainder of the list (starting at the second
position and advancing each time).
Heap sort
• Heap sort is a much more effective type of selection sort, and is one of the
fastest sorting algorithms.
• Programmers often use this when faced with very large arrays they know to
be in an unsorted state.
• The largest or smallest element of the list is determined and then placed at
the end or beginning of the list.
• This process continues with the rest of the list, and is accomplished by using
a data structure called a heap, which is a special type of binary tree.
• Once the data list has been made into a heap, the root node is guaranteed
to be the largest element. When it is removed and placed at the end of the
list, the heap is rearranged so the largest element remaining moves to the
root.
Searching
• Computer systems are often used to store large amounts of data from
which individual records must be retrieved according to some search
criterion.
• Thus the efficient storage of data to facilitate fast searching is an
important issue.
Sequential searching
• The simplest type of searching process is the sequential search or
linear search.
• In the sequential search, each element of the array is compared to
the key, in the order it appears in the array, until the first element
matching the key is found.
• If you are looking for an element that is near the front of the array,
the sequential search will find it quickly.
• The more data that must be searched, the longer it will take to find
the data that matches the key using this process.
Sequential searching
• For a list with n items, the best case is when the value is equal to the
first element of the list, in which case only one comparison is needed.
• The worst case is when the value is not in the list (or occurs only once
at the end of the list), in which case n comparisons are needed.
• The input to a search algorithm is an array of objects A, the number of
objects n, and the key value being sought x.
Binary search
• Binary search is one of the fastest ways to search the element in a
sorted array.
• The idea is to look at the element in the middle.
• If the key is equal to that element, then the search is finished.
• If the key is less than the middle element, do a binary search on the
first half.
• If it's greater than the middle element , do a binary search of the
second half.
Binary search
• The advantage of a binary search over a linear search is, amazing
when you apply binary search for large numbers.
• For an array of a million elements, binary search, O(log N), will find
the target element with a worst case of only 20 comparisons.
• Linear search, O(N), on average will take 500,000 comparisons to find
the element.
• But to apply binary search on a particular array, it must be sorted first.
Because sorting is not a fast operation, it may not be worth the effort
to sort when there are only a few searches.

You might also like