+ All Categories
Home > Documents > Sorting Algorithms

Sorting Algorithms

Date post: 14-Nov-2014
Category:
Upload: jennyrosesison
View: 27 times
Download: 1 times
Share this document with a friend
16
Sorting Algorithms • A process of arranging data or records in a sequence.
Transcript
Page 1: Sorting Algorithms

Sorting Algorithms

• A process of arranging data or records in a sequence.

Page 2: Sorting Algorithms

Types of Sorting

Page 3: Sorting Algorithms

Shell sort

• Invented in 1959 by Donald Shell, the shell sort is a relatively fast algorithm and is easy to code. It attempts to roughly sort the data first, moving large elements towards one end and small elements towards the other. It performs several passes over the data, each finer than the last. After the final pass, the data is fully sorted.

• It is important to note that the shell sort algorithm does not actually sort the data itself; it increases the effeciency of other sorting algorithms. By roughly grouping the data, it reduces the number of times the data elements need to be rearranged in the sort process. Usually an insertion or bubble sort is used to arrange the data at each step, but other algorithms can be used. Shell sort does not noticably benefit the faster algorithms (such as merge and quicksort), and in some cases will actually reduce their performance.

Page 4: Sorting Algorithms

Shell sortAs mentioned above, we will be using an initial value of 3 for n.

Page 5: Sorting Algorithms
Page 6: Sorting Algorithms

Shell sort

Page 7: Sorting Algorithms

Mergesort

• Is a recursive sorting method.• Easiest to state as recursive algorithm.• Is an example of a divide-and-conquer

algorithm.

Page 8: Sorting Algorithms

Types of Mergesorting

• File mergingis basic operation in mergesort.

• Binary mergesortA version of mergesort that does not present any

serious difficulties in designing the required split and merge algorithms.

• Natural mergesorta version of mergesort that take advantage of these

“natural” sorted subfiles.

Page 9: Sorting Algorithms

Psuedocode for file mergingMerge/** Input: Sorted files File1 and File2.

Function: Merges sorted files File1 and File2, giving File3. Output: File3.*/

1. Open file1 and file2 for input, file3 for output.2. Read the first element X from file1 and the first element Y from file2.3. Repeat the following until the end of either file1 or file2 is reached:

If X<Y, theni. Write X to file3.ii. Read a new X value from file2.

Otherwise:i. Write Y to file3.ii. Read a new Y value from file3.

4. If the end of file1 was encountered, copy any remaining elements from file2 into file3. If the end of file2 was encountered, copy the rest of file1 into file3.

Page 10: Sorting Algorithms

Quick Sort• A sorting technique that sequences a list by continuously dividing the list

into two parts and moving the lower items to one side and the higher items to the other.

• It starts by picking one item in the entire list to serve as a pivot point.• The pivot could be the first item or a randomly chosen one. • All items that compare lower than the pivot are moved to the left of the

pivot; all equal or higher items are moved to the right. • It then picks a pivot for the left side and moves those items to left and

right of the pivot and continues the pivot picking and dividing until there is only one item left in the group. It then proceeds to the right side and performs the same operation again.

Page 11: Sorting Algorithms

Example

Page 12: Sorting Algorithms

Radix Sort• is a sorting algorithm that sorts integers by processing individual digits. • Because integers can represent strings of characters (e.g., names or dates)

and specially formatted floating point numbers, radix sort is not limited to integers.

• A least significant digit (LSD) radix sort is a fast stable sorting algorithm which can be used to sort keys in lexicographic order.

• Keys may be a string of characters, or numerical digits in a given 'radix'. • The processing of the keys begins at the least significant digit (i.e., the

rightmost digit), and proceeds to the most significant digit (i.e., the leftmost digit).

Page 13: Sorting Algorithms

• unsorted list: 523 153 088 554 235 • sorting for Radix 0 (least significant digit) 523 153 554 235 088 ^

Page 14: Sorting Algorithms

• sorting for Radix 1 (2nd. significant digit) 523 235 153 554 088 ^ • sorting for Radix 2 (most. significant digit) 088 153 235 523 554 ^

Page 15: Sorting Algorithms

Bubble Sort• Is a simple sorting algorithm. It works by repeatedly

stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order.

• The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

• The algorithm gets its name from the way smaller elements "bubble" to the top of the list. Because it only uses comparisons to operate on elements, it is a comparison sort.

Page 16: Sorting Algorithms

int array[]={76, 3, 94, 55, 21, 1}; int a, b, temp;

for(a=0; a<5; a++) for(b=a+1; b<6; b++)

if(array[a]>array[b]){ temp = array[b]; array[b] = array[a]; array[a] = temp;}

for(a=0; a<6; a++)printf (“%d\t”, array[a]);

}


Recommended