Java project with implemented Merge, Quick and Shell sorts for JSON

Java project with implemented Merge, Quick and Shell sorts for JSON
05/01/2019 Ernestas Konopliovas
In Back-end, Java

Java project with implemented Merge, Quick and Shell sorts for JSON objects. Sorting algorithms are universal and can be reused with any other objects just by creating new Comparator.

Code, I used

Merge sort for objects:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

class MergeSort {                                           //package-private nes mazas projektas, nera kitu packegu

    private List array;
private List TempArray = new ArrayList<>();
private Comparator cmp;

    public void PrepareForSort(List arr, Comparator cmp){    //public, kad butu pasiekiama is Main (ar is kitur)
this.cmp = cmp;
this.array = arr;
doMergeSort(0,arr.size()-1);   //-1, nes dirbame su indeksais, o size(); grazina elementu skaiciu
}

    private void doMergeSort(int LowerIndex, int HigherIndex){       //private, nes naudojamas tik klaseje
if(LowerIndex < HigherIndex) {                               //papildomai dar patikrina, ar elementas nera vienas ir ar su indeksais viskas ok
int middle = LowerIndex + (HigherIndex - LowerIndex) / 2;     //randa arrajaus viduri
doMergeSort(LowerIndex, middle);                         //randa kairiojo arrajaus viduri
doMergeSort(middle + 1, HigherIndex);         //randa desiniojo arrajaus viduri , middle+1, nes tai desiniosios dalies pradzia
MergePart(LowerIndex,middle,HigherIndex);                //tesia darba metodas MergePart()
}
}

    private void MergePart(int LowerIndex, int middle, int HigherIndex) throws NullPointerException{   //private, nes naudojamas tik klaseje

        for(int i = LowerIndex; i<= HigherIndex; i++) {
if(array.get(i) != null)
TempArray.add(i, array.get(i));              //i TempArray "kopijuojamas" array reiksmes. Ne rodykle
else throw new NullPointerException();
}
int i = LowerIndex;                                  //kairiosios dalies pradzia
int j = middle+1;                                    //desiniosios dalies pradzia (del to ir +1) + while dirbs iki middle
int k = LowerIndex;                                  //indeksas, kuris bus naudojamas ruosiavimui (i ji bus dedamos galutinai surusiuotos reiks)
while (i<=middle && j <=HigherIndex){                //while(kairioji dalis nepasieke galo IR desionioji dalis nepasieke galo)
if(cmp.compare(TempArray.get(i), array.get(j)) < 0){     //komparatorius lygina 2 reiksmes pagal uzduota Comparatoriu
// array[k] = TempArray[i];
array.set(k,TempArray.get(i));              //i reiksme is TempArray dedama i array (galutinai rusiuojama)
i++;                                        //indeksas i padidinamas, reiskia juda kairioji dalis
} else{
// array[k] = TempArray[j];
array.set(k,TempArray.get(j));              //j reiksme is TempArray dedama i array (galutinai rusiuojama)
j++;                                        //indeksas j padidinamas, reiskia juda desinioji dalis
}
k++;                                            //k padidinams, nes buvo idetas elementas (bet kokiu atveju)
}
while (i<=middle){                                  //sudedamos visos kitos reiksmes
// array[k] = TempArray[i];
array.set(k,TempArray.get(i));
k++;                                            //atitinkamai padidinami indeksai, kad elementai eitu paeiliui
i++;
}
}

}

Shell sort for objects:

import java.util.Comparator;
import java.util.List;

public class ShellSort {
private Comparator cmp;

    public void quickSort(List arr, int begin, int end, Comparator cmp) {
this.cmp = cmp;
if (begin < end) {
int partitionIndex = partition(arr, begin, end);

            quickSort(arr, begin, partitionIndex-1, cmp);
quickSort(arr, partitionIndex+1, end,cmp);
}
}

    private int partition(List arr, int begin, int end) {
Object pivot = arr.get(end);
int i = (begin-1);

        for (int j = begin; j < end; j++) {
if (cmp.compare(arr.get(j), pivot) < 0){ i++; Object swapTemp = arr.get(i); arr.set(i,arr.get(j)); arr.set(j,swapTemp); } } Object swapTemp = arr.get(i+1); arr.set(i+1,arr.get(end)); arr.set(end,swapTemp); return i+1; } public void sort(List arr, Comparator cmp) { this.cmp = cmp; int n = arr.size(); for (int gap = n/2; gap > 0; gap /= 2)
{

            for (int i = gap; i < n; i += 1) { Object temp = arr.get(i); int j; for (j = i; j >= gap && cmp.compare(arr.get(j-gap), temp) > 0; j -= gap)
arr.set(j,arr.get(j-gap));

                arr.set(j,temp);
}
}
}

}

Quick sort for objects:

import java.util.Comparator;
import java.util.List;

public class QuickSort {
private Comparator cmp;

    public void quickSort(List arr, int begin, int end, Comparator cmp) {
this.cmp = cmp;
if (begin < end) {
int partitionIndex = partition(arr, begin, end);

            quickSort(arr, begin, partitionIndex-1, cmp);
quickSort(arr, partitionIndex+1, end,cmp);
}
}

    private int partition(List arr, int begin, int end) {
Object pivot = arr.get(end);
int i = (begin-1);

        for (int j = begin; j < end; j++) {
if (cmp.compare(arr.get(j), pivot) < 0){
i++;

                Object swapTemp = arr.get(i);
arr.set(i,arr.get(j));
arr.set(j,swapTemp);
}
}

        Object swapTemp = arr.get(i+1);
arr.set(i+1,arr.get(end));
arr.set(end,swapTemp);

        return i+1;
}
}

Comments (0)

Leave a reply

Your email address will not be published. Required fields are marked *

*