What's new

Closed Java Programming - other Sorting Program

Status
Not open for further replies.
Joined
Jun 23, 2013
Posts
20
Reaction
16
Points
69
Age
27
Hi Guys! kailangan kasi namin ng program.. pede ba kau mag suggest ng ibang codes regarding sorting.

class SortQuick
{
public static void main(String[] args)
{
int i;
int array[] = {12,9,4,99,1};
System.out.println("Elements Before Sort:\n");
for(i = 0; i < array.length;i++)
System.out.print(array+" ");
System.out.println();
quickSort(array,0,array.length-1);
System.out.println("Elements Before Sort:\n");
for(i = 0;i<array.length;i++)
System.out.print(array+" ");
System.out.println();
}
public static void quickSort(int a[],int low,int len)
{
if(low>=len)return;
int l=low,n=len;
int piv=a[(l+n)/2];
while(l<n)
{/**
moving upto less than pivot value from start.*/
while(l<n&&a[l]<piv)
l++;
/**
moving upto greater than pivot value from end*/
while(l<n&&a[n]>piv)
n--;
/**
swap in order to move least elements to left and maximum to right of the pivot*/
if(l<n)
{
int tem = a[l];
a[l]=a[n];
a[n]=tem;
}//end of while loop ------------1
if(n<l)//Checkingstart and end index(start must be less than end otherwise swap)
{
int t = l;l=n;n=t;
}
quickSort(a,low,l);
quickSort(a,l==low?l+1:l,len);
}
}
}

I need Program other than this Thanks :)
 

Attachments

Simple Sorting algo: Bubble Sort, Selection sort, Insertion sort.
Advance Sorting : Radix Sort, Shell Sort, Quick Sort.
 
implement Merge Sort


Code:
package com.java2novice.sorting;
public class MyMergeSort {
     
    private int[] array;
    private int[] tempMergArr;
    private int length;
    public static void main(String a[]){
         
        int[] inputArr = {45,23,11,89,77,98,4,28,65,43};
        MyMergeSort mms = new MyMergeSort();
        mms.sort(inputArr);
        for(int i:inputArr){
            System.out.print(i);
            System.out.print(" ");
        }
    }
     
    public void sort(int inputArr[]) {
        this.array = inputArr;
        this.length = inputArr.length;
        this.tempMergArr = new int[length];
        doMergeSort(0, length - 1);
    }
    private void doMergeSort(int lowerIndex, int higherIndex) {
         
        if (lowerIndex < higherIndex) {
            int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
            // Below step sorts the left side of the array
            doMergeSort(lowerIndex, middle);
            // Below step sorts the right side of the array
            doMergeSort(middle + 1, higherIndex);
            // Now merge both sides
            mergeParts(lowerIndex, middle, higherIndex);
        }
    }
    private void mergeParts(int lowerIndex, int middle, int higherIndex) {
        for (int i = lowerIndex; i <= higherIndex; i++) {
            tempMergArr[i] = array[i];
        }
        int i = lowerIndex;
        int j = middle + 1;
        int k = lowerIndex;
        while (i <= middle && j <= higherIndex) {
            if (tempMergArr[i] <= tempMergArr[j]) {
                array[k] = tempMergArr[i];
                i++;
            } else {
                array[k] = tempMergArr[j];
                j++;
            }
            k++;
        }
        while (i <= middle) {
            array[k] = tempMergArr[i];
            k++;
            i++;
        }
    }
}
 
Status
Not open for further replies.

Similar threads

Back
Top