Reprint Please specify:
View animation learning algorithms and data structures (not updated periodically) ()
Quick Sort (QuickSort)
Animated Demo:
Java code:
public class QuickSort {private int array[];p rivate int length;public void sort (int[] inputarr) {if (Inputarr = = NULL | | i Nputarr.length = = 0) {return;} This.array = Inputarr;length = Inputarr.length;quicksort (0, length-1);} private void QuickSort (int lowerindex, int higherindex) {int i = lowerindex;int j = higherindex;//Calculate pivot number, I am taking pivot as middle index numberint pivot = array[lowerindex + (higherindex-lowerindex)/2];//Divide into II Arrayswhile (i <= j) {while (Array[i] < pivot) {i++;} while (Array[j] > Pivot) {j--;} if (i <= j) {Exchangenumbers (i, j);//move index to next position on both sides i++;j--;}} Call the QuickSort method if (Lowerindex < J) QuickSort (Lowerindex, J), if (I < Higherindex) QuickSort (i, Higherindex);} private void exchangenumbers (int i, int j) {int temp = Array[i];array[i] = array[j];array[j] = temp;} public static void Main (String a[]) {QuickSort sorter = new QuickSort (); int[] input = {3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 48};sorter.sort (input), for (int i:input) {System.out.print (i+ ",");}}}
Compilation Result:
Count Sort (countingsort)
Animated Demo:
Java code:
public class Countingsort {public static void main (String []args) {//sorted array int a[] = {6,3,1,2,5,7,8,2,1,2,9,5};int b[] = cou Ntsort (a); for (int i:b) {System.out.print (i + "");}} public static int[] Countsort (int []a) {int b[] = new Int[a.length];int max = a[0], min = a[0];for (int i:a) {if (i > Max ) {max = i;} if (i < min) {min = i;}} Here k is the size of the array to be sorted, the value difference of the element size +1int k = max-min + 1;int c[] = new Int[k];for (int i = 0; i < a.length; ++i) {C[a[i]-min] + = 1;//optimized places, reducing the size of the array c}for (int i = 1; i < c.length; ++i) {c[i] = C[i] + c[i-1];} for (int i = a.length-1; I >= 0; i) {b[--c[a[i]-min]] = a[i];//extracts the element of C by means of Access}return B;}}
Compilation Result:
Base sort (Radix sort)
Animated Demo:
Java code to defragment
2015-2-24
View animation learning algorithms and data Structures (ii) (<garry (iv) >)