Você está na página 1de 4

1

Introduction

Sorting is rearranging the elements in a data structure (array or linked list) so that after the procedure is done, they are ranked by their size (in either ascending or descending order).

Selection Sort

Selection sort is one of the algorithms used to solve problems, sorting data in an array. Selection type is among the best of sorting techniques and it work ne for little les.

The steps in selection sort: The rst step is to nd smallest element from the array. Then the smallest element is exchanged with the rst element. Thus the rst element now has the smallest value compared to other element. The second step we nd the smallest data from the array up to the last second. Data smallest interchangeable with the element we have obtained the second and so forth until all elements of the array is sorted. Analysis of Selection Sort O(N 2 ) for worst and average cases Uses about N 2 /2 comparisons Uses about n exchanges It is a good technique for elements heavy to move, but easy to compare. However, the number of data movements is the number of swaps required, which is N 1. This algorithm is very similar to the insertion sort algorithm. It requires fewer data movements, but requires more comparisons (Algorithms and Data Structures 1995-2000 Alfred Strohmeier).

Insertion Sort

Insertion sort is a simple sorting algorithm: a comparison sort in which the sorted array is built one entry at a time. The insertion sort is also astable sort. Stable sorts retain the original ordering of keys when identical keys are present in the input data. Each element in the array are examined one by one. The larger element are moved to the right of the array and the smaller element are placed in its correct position with the element already examined. Analysis of Insertion Sort: The best case input is an array that is already sorted. In this During each iteration, the rst remaining element of the input is only compared with the right-most element of the sorted subsection of the array (about N comparisons, 0 moves). Worst case: about N 2 /2 comparisons,N 2 /2 moves, (input sorted in reverse order). Average case: about N 2 /4 comparisons, N 2 /4 moves. 3

The insertion sort is an in-place sort. That is, we sort the array in-place. No extra memory is required. Moreover, it is very ecient when input is almost sorted.

Bubble Sort

Bubble Sort algorithm sorts the elements by bubbling up the largest element to the end of the array. In all, the bubble process (inside j loop) is repeated N-1 times for an array of size N. The steps in bubble sort: compare each pair of adjacent elements from the beginning of an array and if they are in reverse order, swap them. continue the above step with the next adjacent pair. Analysis of Bubble Sort: To begin with the inside loop does N 1 comparisons, next time N 2 and so on. Finally on the last iteration of the outside loop, the 4

inside loop does 1 comparison. So on average the inside loop does N/2 comparisons. number of computation steps is N N/2 = N 2 /2 Complexity of bubble sort = O(N 2 )

Você também pode gostar