Sei sulla pagina 1di 16

Sorting Algorithms

• A process of arranging data or


records in a sequence.
Types of Sorting
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.
Shell sort
As mentioned above, we will be using an initial value of 3 for n.
Shell sort
Mergesort
• Is a recursive sorting method.
• Easiest to state as recursive
algorithm.
• Is an example of a divide-and-
conquer algorithm.
Types of Mergesorting
• File merging
is basic operation in mergesort.
• Binary mergesort
A version of mergesort that does not present
any serious difficulties in designing the required
split and merge algorithms.
• Natural mergesort
a version of mergesort that take advantage of
these “natural” sorted subfiles.
Psuedocode for file
merging
Merge
/** Input: Sorted files File1 and File2.
Function: Merges sorted files File1 and File2, giving
File3.
Output: File3.*/
5. Open file1 and file2 for input, file3 for output.
6. Read the first element X from file1 and the first element
Y from file2.
7. Repeat the following until the end of either file1 or file2
is reached:
If X<Y, then
i. 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.
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.
Example
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).
• unsorted list:
523
153
088
554
235
• sorting for Radix 0 (least significant digit)
523
153
554
235
088
^
• sorting for Radix 1 (2nd. significant digit)
523
235
153
554
088
^
• sorting for Radix 2 (most. significant digit)
088
153
235
523
554
^
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.
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]);
}

Potrebbero piacerti anche