Anda di halaman 1dari 5

Algorithms and Data Structures

Sorting and Searching Algorithms

Sorting Algorithms

Insertion sort
A simple sorting algorithm A comparison sort in which the sorted array is built one entry at a time It is much less efficient on large lists

Algorithm
In abstract terms, every iteration of the insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list In the best case of an already sorted array, this implementation of insertion sort takes O(n) time in each iteration The worst case is an array sorted in reverse order, it takes O(n2)

Explanation
Let a0,.,an-1 be the sequence to be sorted. After each iteration of the algorithm the sequence consists of two parts: the first part a0,.,ai-1, is already sorted, the second part ai,.,an-1 is still unsorted where i0,.,n

Explanation
In order to insert element ai into the sorted part, it is compared with ai-1, ai-2 etc. when an element aJ with aJ ai is found, ai is inserted behind it. If no such element is found, then ai is inserted at the beginning of the sequence After inserting element ai the length of the sorted part has increased by one In the next iteration, ai+1 is inserted into the sorted part

Example
The following table shows the steps for sorting the sequence 5 7 0 3 4 2 6 1 The sorted part is shown in red

Example (Cont)
5 5 0 0 0 0 0 0 7 7 5 3 3 2 2 1 0 0 7 5 4 3 3 2 3 3 3 7 5 4 4 3 4 4 4 4 7 5 5 4 2 2 2 2 2 7 6 5 6 6 6 6 6 6 7 6 1 1 1 1 1 1 1 7

Selection Sort
A sorting algorithm, specifically is an in-place comparison sort It has O(n2) complexity, making it inefficient on large lists It works as follows:
Find the minimum value in the list Swap it with the value in the first position Repeat these steps for remainder of the list

Selection Sort
The list is divided into two parts:
1. the sublist of items that are already sorted 2. The sublist of items remaining to be sorted
Sorted list Unsorted list

Example
The list below is to be sorted using selection sort algorithm 31 25 12 22 11 11 25 12 22 31 11 12 25 22 31 11 12 22 25 31

Shell Sort
Insertion sort can move entries only one position, that is it compares only adjacent keys If we were to modify it so that it first compares keys far apart, then it could sort the entries far apart. Afterward, the entries closer together would be sorted, and finally the increment between keys being compared would be reduced to 1, to ensure that the list is completely in order The last step of shell sort is an insertion sort , by then the array of data is guaranteed to be almost sorted

Gap Sequence
The gap sequence is an integral part of the shell sort algorithm. Any increment sequence will work, so long as the last element is 1. The algorithm begins by performing a gap insertion sort, with the gap being the first number in the gap sequence. It continues to perform a gap insertion sort for each number in the sequence, until it finishes with a gap of 1.

When the gap is 1, the gap insertion sort is simply an ordinary insertion sort, guaranteeing that the final list is sorted. The gap sequence that was originally suggested by Donald Shell was to begin with N / 2 and to halve the number until it reaches 1.

Example Searching Algorithms

Introduction and Notation


Information retrieval is one of the most important applications of computers we are given one piece of information, which we shall call a key, and we are asked to find a record that contains other information associated with the key. We shall allow both the possibility that there is more than one record with the same key and that there is no record at all with a given key.

Analysis
Searching for the keys that locate records is often the most time-consuming action in a program, and, therefore, the way the records are arranged and the choice of method used for searching can make a substantial difference in the programs performance. For this reason, we shall study how much work is done by each of the algorithms we develop. Counting the number of times that one key is compared with another gives us an excellent measure of the total amount of work that the algorithm will do and of the total amount of computer time it will require when it is run.

Minimal Standards
search a contiguous list of records that is generally called the list. Every record is associated to a key. Keys can be compared for equality or relative ordering. Records can be compared to each other or to keys by first converting records to their associated keys.

Sequential Search
In computer science, sequential search is a search algorithm, also known as linear search, that is suitable for searching a set of data for a particular value. It operates by checking every element of a list one at a time in sequence until a match is found. Sequential search runs in O(N). If the data are distributed randomly, on average (N+1)/2 comparisons will be needed. The best case is that the value is equal to the first element tested, in which case only 1 comparison is needed. The worst case is that the value is not in the list (or is the last item in the list), in which case N comparisons are needed.

Simplicity of sequential search


The simplicity of the sequential search means that if just a few elements are to be searched, it is less trouble than more complex methods that require preparation, such as sorting the list to be searched or more complex data structures, especially when entries may be subject to frequent revision. Another possibility is when certain values are much more likely to be searched for than others and it can be arranged that such values will be among the first considered in the list.

Source Code
#include <iostream.h> int LinearSearch(int [], int, int); int main() { const int NUMEL = 10; int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101}; int item, location; cout << "Enter the item you are searching for: "; cin >> item; location = LinearSearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location << endl; else cout << "The item was not found in the list\n"; return 0; }

Contd
// this function returns the location of key in the list //a -1 is returned if the value is not found int LinearSearch(int list[], int size, int key) { int i; for (i = 0; i < size; i++) { if (list[i] == key) return i; } return -1; }

Binary Search
A binary search algorithm (or binary chop) is a technique for finding a particular value in a sorted list. It makes progressively better guesses, and closes in on the sought value by selecting the median element in a list, comparing its value to the target value, and determining if the selected value is greater than, less than, or equal to the target value. A guess that turns out to be too high becomes the new top of the list, and a guess that is too low becomes the new bottom of the list. Pursuing this strategy iteratively, it narrows the search by a factor of two each time, and finds the target value.

Detailed View
Our binary search algorithm will use two indices, top and bottom, to enclose the part of the list in which we are looking for the target key. At each iteration, we shall reduce the size of this part of the list by about half. The target key, provided it is present in the list, will be found between the indices bottom and top, inclusive. We establish the initial correctness of this assertion by setting bottom to 0 and top to (list size 1). To do binary search, we first calculate the index mid halfway between bottom and top as mid = (bottom + top)/2

Contd
Next, we compare the target key against the key at position mid and then we change the appropriate one of the indices top or bottom so as to reduce the list to either its bottom or top half. Next, the binary search should terminate when top bottom; that termination is, when the remaining part of the list contains at most one item. we must make a progress toward termination by ensuring that the number of items remaining to be searched, top bottom + 1, strictly decreases at each iteration of the process. Several slightly different algorithms for binary search can be written.

Source Code
#include <iostream.h> int BinarySearch(int [], int, int); int main() { const int NUMEL = 10; int nums[NUMEL] = {5,10,22,32,45,67,73,98,99,101}; int item, location; cout << "Enter the item you are searching for: "; cin >> item; location = BinarySearch(nums, NUMEL, item); if (location > -1) cout << "The item was found at index location " << location << endl; else cout << "The item was not found in the list\n"; return 0; }

Contd
int BinarySearch(int list[], int size, int key) { int left, right, midpt; left = 0; right = size - 1; while (left <= right) { midpt = (int) ((left + right) / 2); if (key == list[midpt]) { return midpt; } else if (key > list[midpt]) left = midpt + 1; else right = midpt - 1; } return -1; }

The End

Anda mungkin juga menyukai