Sort methods in java


public class Sorts {
/**
 * @author Alihan98ersoy
 * 01.07.2017(month,day,year)
 * Contents:
 * Selection Sort
 * İnsertion Sort
 * Shell Sort
 * Quick Sort
 */

//Selection sort
//Find smallest number in second for replace it with first for integer i.

public void Selectionsort(int[]list){

for(int i=0;i<list.length;i++){
int indexofsmallestnumber=i;

for(int j=i+1;j<list.length;j++){
if(list[j]<list[indexofsmallestnumber])  {indexofsmallestnumber=j;}
}
int basket=list[indexofsmallestnumber];//basket for replace
list[indexofsmallestnumber]=list[i];
list[i]=basket;
}
}


 /*İnsertion sort
*İnsertion is good for small elements only because it requires more time for sorting a large number of elements.
*Find the smallest number and put it correct place with comparing it every to until finding the correct place.
*insertionSort works pretty well for partially sorted arrays!! */

  public void insertionSort(int[] list) {
     
        for (int i = 1; i < list.length; i++) {
            for (int j = i; j > 0 && (list[j]< list[j - 1]); j--) {
//(list[j]> list[j - 1]) if you change this part like this you can sort biggest to smallest
                int basket=list[j];//basket for replace
                list[j]=list[j-1];
                list[j-1]=basket;
            }
        }
    }


//Shell Sort
 //According to https://github.com/uzay00/CMPE211/blob/master/Lecture4/IntermediateSorts.java
//Shell sort using insertion sort for creating partially sorted array and
//it's sorting all array with insertion sort

  public static void shellSort(int[] list){
        int N = list.length, h = 1;
        while (h < N/3) h = 3*h + 1; // 1, 4, 13, 40, 121, 364, 1093, ...
        // For different h values we sort array a, result will be a partially sorted array
        while (h > 1){
   
            for (int i = h; i < N; i++) { // Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]... .
                for (int j = i; j >= h && (list[j]< list[j - h]); j -= h) {
                    //Exchanges takes places between every h distant pairs, not only adjacent items
                int basket=list[j];//basket for replace
                list[j]=list[j-h];
                list[j-h]=basket;
                }}
            h = h/3;
        }
        // Final touch is the original insertion sort..
            h=1;
            for (int i = h; i < N; i++) { // Insert a[i] among a[i-h], a[i-2*h], a[i-3*h]... .
                for (int j = i; j >= h && (list[j]< list[j - h]); j -= h) {
                    //Exchanges takes places between every h distant pairs, not only adjacent items
                int basket=list[j];//basket for replace
                list[j]=list[j-h];
                list[j-h]=basket;
                }
            }
    }

//Quick Sort
//According to http://www.vogella.com/tutorials/JavaAlgorithmsQuicksort/article.html
int[] numbers;
     int number;
public void quicksort(int[] values) {
        // check for empty or null array
        if (values ==null || values.length==0){
            return;
        }
        numbers = values;
         number = values.length;
         sort(0, number - 1);
    }

    private void sort(int low, int high) {
        int i = low, j = high;
        // Get the pivot element from the middle of the list
        int pivot = numbers[low + (high-low)/2];

        // Divide into two lists
        while (i <= j) {
            // If the current value from the left list is smaller than the pivot
            // element then get the next element from the left list
            while (numbers[i] < pivot) {
                i++;
            }
            // If the current value from the right list is larger than the pivot
            // element then get the next element from the right list
            while (numbers[j] > pivot) {
                j--;
            }

            // If we have found a value in the left list which is larger than
            // the pivot element and if we have found a value in the right list
            // which are smaller than the pivot element then we exchange the
            // values.
            // As we are done we can increase i and j
            if (i <= j) {
                exchange(i, j);
                i++;
                j--;
            }
        }
        // Recursion
        if (low < j)
        sort(low, j);
        if (i < high)
        sort(i, high);
    }
    private void exchange(int i, int j) {
        int temp = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = temp;
    }

//MergeSort




       //Print method for integer array
public void printarray(int[]array){
for(int i=0;i<array.length;i++){
System.out.println("Sıra: "+i+"\t"+"Sayı: "+array[i]);
}
}
public static void main(String[] args) {
Sorts a=new Sorts();
int[]array={3,2,1,5,6,9,8,10,4,7};
//a.Selectionsort(array);
//a.insertionSort(array);
//a.shellSort(array);
//a.quicksort(array);
a.printarray(array);


}

}


pictures from:
http://www.java2novice.com/java-sorting-algorithms/selection-sort/
https://www.javatpoint.com/insertion-sort-in-java
https://dzone.com/articles/algorithm-week-shell-sort
https://commons.wikimedia.org/wiki/File:Merge_sort_algorithm_diagram.svg

java file:
https://drive.google.com/open?id=14ItXSLyjnSNW-ZzozSTVVbDMN8eVFIby





Yorumlar

Bu blogdaki popüler yayınlar

Engelsizkitap

Linear and Binary search methods in java