Вы находитесь на странице: 1из 14

Table of Contents

10 Sorting Algorithm

3 Best Sorting Algorithm with the Program

The Best Sorting Algorithm for Me

10 SORTING ALGORITHM 1. Selection sort Main article: Selection sort Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has O(n2) complexity, making it inefficient on large lists, and generally performs worse than the similarinsertion sort. Selection sort is noted for its simplicity, and also has performance advantages over more complicated algorithms in certain situations. The algorithm finds the minimum value, swaps it with the value in the first position, and repeats these steps for the remainder of the list. It does no more than n swaps, and thus is useful where swapping is very expensive. 2. Insertion sort Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list. In arrays, the new list and the remaining elements can share the array's space, but insertion is expensive, requiring shifting all following elements over by one. Shell sort (see below) is a variant of insertion sort that is more efficient for larger lists.

3. Shell sort Shell sort was invented by Donald Shell in 1959. It improves upon bubble sort and insertion sort by moving out of order elements more

than one position at a time. One implementation can be described as arranging the data sequence in a two-dimensional array and then sorting the columns of the array using insertion sort. 4. Timsort Timsort finds runs in the data, creates runs with insertion sort if necessary, and then uses merge sort to create the final sorted list. It has the same complexity (O(nlogn)) in the average and worst cases, but with pre-sorted data it goes down to O(n). 5. Radix sort Radix sort is an algorithm that sorts numbers by processing individual digits. n numbers consisting of k digits each are sorted in O(n k) time. Radix sort can either process digits of each number starting from the least significant digit (LSD) or the most significant digit (MSD). The LSD algorithm first sorts the list by the least significant digit while preserving their relative order using a stable sort. Then it sorts them by the next digit, and so on from the least significant to the most significant, ending up with a sorted list. While the LSD radix sort requires the use of a stable sort, the MSD radix sort algorithm does not (unless stable sorting is desired). In-place MSD radix sort is not stable. It is common for the counting sort algorithm to be used internally by the radix sort. Hybrid sorting approach, such as using insertion sort for small bins improves performance of radix sort significantly. 6. Bucket sort Bucket sort is a divide and conquer sorting algorithm that generalizes Counting sort by partitioning an array into a finite number of buckets. Each bucket is then sorted individually, either using a different sorting

algorithm, or by recursively applying the bucket sorting algorithm. A variation of this method called the single buffered count sort is faster than quicksort and takes about the same time to run on any set of data. Due to the fact that bucket sort must use a limited number of buckets it is best suited to be used on data sets of a limited scope. Bucket sort would be unsuitable for data such as social security numbers - which have a lot of variation. 7. Counting Sort Counting sort is applicable when each input is known to belong to a particular set, S, of possibilities. The algorithm runs in O(|S| + n) time and O(|S|) memory where n is the length of the input. It works by creating an integer array of size |S| and using the ith bin to count the occurrences of the ith member of S in the input. Each input is then counted by incrementing the value of its corresponding bin. Afterward, the counting array is looped through to arrange all of the inputs in order. This sorting algorithm cannot often be used because S needs to be reasonably small for it to be efficient, but the algorithm is extremely fast and demonstrates great asymptotic behavior as n increases. It also can be modified to provide stable behavior.

8. Quicksort Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array, we choose an element, called a pivot, move all smaller elements before the pivot, and move all greater elements after it. This can be done efficiently in linear time and inplace. We then recursively sort the lesser and greater sublists. Efficient

implementations of quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex, but are among the fastestsorting algorithms in practice. Together with its modest O(log n) space usage, this makes quicksort one of the most popular sorting algorithms, available in many standard libraries. The most complex issue in quicksort is choosing a good pivot element; consistently poor choices of pivots can result in drastically slower O(n) performance, but if at each step we choose themedian as the pivot then it works in O(n log n). Finding the median, however, is an O(n) operation on unsorted lists, and therefore exacts its own penalty. 9. Merge sort Merge sort takes advantage of the ease of merging already sorted lists into a new sorted list. It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the first should come after the second. It then merges each of the resulting lists of two into lists of four, then merges those lists of four, and so on; until at last two lists are merged into the final sorted list. Of the algorithms described here, this is the first that scales well to very large lists, because its worst-case running time is O(n log n). Merge sort has seen a relatively recent surge in popularity for practical implementations, being used for the standard sort routine in the programming languages Perl, Python (as timsort), and Java (also uses timsort as ofJDK7), among others. Merge sort has been used in Java at least since 2000 in JDK1.3. 10. Comb sort Comb sort is a relatively simplistic sorting algorithm originally designed by Wlodzimierz Dobosiewicz in 1980. Later it was rediscovered and popularized by Stephen Lacey and Richard Boxwith a

Byte Magazine article published in April 1991. Comb sort improves on bubble sort, and rivals algorithms like Quicksort. The basic idea is to eliminate turtles, or small values near the end of the list, since in a bubble sort these slow the sorting down tremendously. (Rabbits, large values around the beginning of the list, do not pose a problem in bubble sort.).

3 best sorting algorithms 1. Quicksort 2. Radix sort 3. Insertion sort

import java.lang.*; public class RadixSort{ public static void main (String []xxx){ int i; int array []= {1,3,4,2,5,7,6,8,10,9,11,13,12,15,14,16,18,17,19,18,20, 21,23,22,24,26,25,28,27,30,29,31,33,32,34,35,37,36,38,40,39,41,43, 42,45,44,47,46,49,48,50,52,51,54,53,57,56,59,58,60,61,63,64,62,65, 67,66,68,70,69, 71,73,74,72,75,77,76,78,80,79,81,83,84,82,85,87,86,88,90,89,91,93,

94,92,95,97,96,98,100,99,150,250,350,450,550,650,750,850,950,15 00,110,210,310,410,510,610,710,810,910,1100,120,220,320,420,520 ,620,720,820,920,1200,130,230,330,430,530,630,730,830,930,1300, 140,240,340,44, 540,640,740,840,940,1400, 160,260,360,460,560,660,760,860,960,1600, 170,270,370,470,570,670,770,870,970,1700,180,280,380,480,580,68 0,780,880,980,1800, 190,290,390,490,590,690,790,890,990,1900,210,220, 230, 240,2550,260,270,280,290,3000};

System.out.println("Values before the sort:ln"); for(i = 0;i <array.length;i++) System.out.println(array[i]+""); System.out.println(); bubble_srt(array,array.length); System.out.println("Values after the sort :ln"); for(i=0;i<array.length;i++) System.out.print(array[i]+" "); System.out.print(" "); System.out.println("Finish"); } public static void bubble_srt(int a [],int n){

int i,j,t; for(i= 0;i<n-1;i++){ for(j=0;j<n-1-i;j++) if (a[j]>a[j+1]){ t = a [j]; a[j]=a[j+1]; a[j+1]= t; } } } } http://www.roseindia.net/java/beginners/arrayexamples/RadixSort.sht ml

import java.io.*; public class QuickSort{ private static int j; public static void main(String args[],int temp)throws IOException{

BufferedReader r=new BufferedReader(new InputStreamReader(System.in));

int a[]=new int [3000]; for(int i=0;i<3000;i++){ System.out.println("Enter a no"); a[i]=Integer.parseInt(r.readLine()); } a[]= {1,3,4,2,5,7,6,8,10,9,11,13,12,15,14,16,18,17,19,18,20, 21,23,22,24,26,25,28,27,30,29,31,33,32,34,35,37,36,38,40,39,41,43, 42,45,44,47,46,49,48,50,52,51,54,53,57,56,59,58,60,61,63,64,62,65, 67,66,68,70,69, 71,73,74,72,75,77,76,78,80,79,81,83,84,82,85,87,86,88,90,89,91,93, 94,92,95,97,96,98,100,99,150,250,350,450,550,650,750,850,950,15 00,110,210,310,410,510,610,710,810,910,1100,120,220,320,420,520 ,620,720,820,920,1200,130,230,330,430,530,630,730,830,930,1300, 140,240,340,44, 540,640,740,840,940,1400, 160,260,360,460,560,660,760,860,960,1600, 170,270,370,470,570,670,770,870,970,1700,180,280,380,480,580,68 0,780,880,980,1800, 190,290,390,490,590,690,790,890,990,1900,210,220, 230, 240,2550,260,270,280,290,3000}; if(a[0]<a[1])

if(a[1]<a[2]) for (int i=0;i<10;i++){ if(int j=1;j<i;j++){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } }

for(int i=0; i<3000; i++){ System.out.println(a[i]); } } } http://www.roseindia.net/java/beginners/arrayexamples/QuickSort.sht ml

public class InsertionSort{ public static void main(String a[]){ int i; int array[] = {1,3,4,2,5,7,6,8,10,9,11,13,12,15,14,16,18,17,19,18,20, 21,23,22,24,26,25,28,27,30,29,31,33,32,34,35,37,36,38,40,39,41,43, 42,45,44,47,46,49,48,50,52,51,54,53,57,56,59,58,60,61,63,64,62,65, 67,66,68,70,69, 71,73,74,72,75,77,76,78,80,79,81,83,84,82,85,87,86,88,90,89,91,93, 94,92,95,97,96,98,100,99,150,250,350,450,550,650,750,850,950,15 00,110,210,310,410,510,610,710,810,910,1100,120,220,320,420,520 ,620,720,820,920,1200,130,230,330,430,530,630,730,830,930,1300, 140,240,340,44, 540,640,740,840,940,1400, 160,260,360,460,560,660,760,860,960,1600, 170,270,370,470,570,670,770,870,970,1700,180,280,380,480,580,68 0,780,880,980,1800, 190,290,390,490,590,690,790,890,990,1900,210,220, 230, 240,2550,260,270,280,290,3000};

System.out.println("Matt John\n"); System.out.println("InsertionSort\n"); System.out.println("Values Before the sort:"); for(i = 0; i < array.length; i++)

System.out.print( array[i]+" "); System.out.println(); insertion_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); }

public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; } array[j] = B; }

} } http://www.roseindia.net/java/beginners/arrayexamples/InsertionSort .shtml

The best sorting algorithm for me is the InsertionSort algorithm. Positive feature of insertion sorting: 1.It is simple to implement 2.It is efficient on (quite) small data values 3.It is efficient on data sets which are already nearly sorted.

The complexity of insertion sorting is O(n) at best case of an already sorted array and O(n2) at worst case .

Code description: In insertion sorting take the element form left assign value into a variable. Then compare the value with previous values. Put value so that values must be lesser than the previous values. Then assign next value to a variable and follow the same steps relatively until the comparison not reached to end of array. Working of insertion sorting:

The steps that I have done to be able to finish this my task: First I search the web for the 10 sorting algorithm, second I choose 3 best sorting algorithm from the 10 sorting algorithm that I have been searched and I give example of a program for each and then I test it. Finally I choose the best of the three sorting algorithm and I came out with Insertion Sort.

Вам также может понравиться