Sie sind auf Seite 1von 3

Different ways of sorting Arrays in Java

Selection sort algorithm


public class SelSort
{
public static void main(String[] args)
{
int[] array = {11, 5, 2, 8, 1, 3, 10, 7, 4, 6, 9, 8};
int n = array.length;
printArray(array);

//**** sorting the array


for (int i=0; i<n; i++)
{
int pos = findMin(array, i); // position of the min element in
if (pos != i) // the subarray array[i .. n]
swap(array, i, pos); // swap the i-th el-t with the minimum
printArray(array); // control output of the modified array
}
}

// this method swaps elements with indices i and j in the array a


public static void swap(int[] a, int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

// this method finds the minimum element in the tail of array a


// starting with index start
public static int findMin(int[] a, int start)
{
int min = a[start];
int index = start;
for (int i=start+1; i<a.length; i++)
if (min > a[i])
{
min = a[i];
index = i;
}
return index;
}

// this method prints all array elements


public static void printArray(int[] a)
{
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
}
}
Bubble sort algorithm
public class BubbleSort
{
public static void main(String[] args)
{
int[] array = {11, 5, 2, 8, 1, 3, 10, 7, 4, 6, 9, 8};
int n = array.length;
printArray(array);

int k = 0; // "bubble" position


int step = 0;
while (k < n-1)
{
step++;
System.out.print("Step # " + step + " k=" + k + " ");
if (array[k] <= array[k+1]) // proceed if two numbers are in order
{
System.out.println("proceed");
k++;
}
else // swap two numbers and start from the beginning
{
System.out.println("swapping " + array[k] + " and " + array[k+1]);
swap(array, k, k+1);
k = 0;
}
printArray(array);
System.out.println("");
}
}

public static void swap(int[] a, int i, int j)


{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}

public static void printArray(int[] a)


{
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
}
}
Merge sort algorithm
public class MergeSort
{
public static void main(String[] args)
{
int[] array = {11, 5, 2, 8, 1, 3, 10, 7, 4, 6, 9, 8};
int n = array.length;
printArray(array);

mergeSort(array, 0, n-1);
printArray(array);
}

// sort the subarray a[i]..a[j]


public static void mergeSort(int[] a, int i, int j)
{
if (i == j) return; // terminating condition
int m = (i+j)/2; // Step 1: compute the splitting point
mergeSort(a, i, m); // Step 2: sort the left part
mergeSort(a, m+1, j); // Step 3: sort the right part
merge(a, i, m, j); // Step 4: merge the sorted parts
}

// merge two subarrays a[i]..a[m] and a[m+1]..a[j] into a single array


// a[i]..a[j]
public static void merge(int[]a, int i, int m, int j)
{
int l = i; // left pointer
int r = m+1; // right pointer
int[] aux = new int[j-i+1]; // auxilary array
int k = 0; // aux pointer

while((l <= m) && (r <= j)) // fill out the aux array
{
if (a[l] < a[r]) // take the minimum of a[l] and a[r]
{
aux[k] = a[l]; // ... and put it into aux
l++; // update the left pointer
k++; // update the aux pointer
}
else
{
aux[k] = a[r];
r++; // update the right pointer
k++; // update the aux pointer
}
}

while (l<=m) // put the rest of the left part to aux


{
aux[k] = a[l];
l++;
k++;
}

while (r<=j) // put the rest of the right part to aux


{
aux[k] = a[r];
r++;
k++;
}

for (k=0; k<aux.length; k++) // save changes in the original array a


{
a[i+k] = aux[k];
}
}

public static void printArray(int[] a)


{
for (int i=0; i<a.length; i++)
System.out.print(a[i] + " ");
System.out.println("");
}
}

Das könnte Ihnen auch gefallen