[关闭]
@xmruibi 2015-03-08T19:03:19.000000Z 字数 2830 阅读 780

Sort Problem

Algorithm


Sort Algorithm Summary:

internal sorts.

• Insertion sort, selection sort, bubblesort, shaker sort.
• Quicksort, mergesort, heapsort, samplesort, shellsort.
• Solitaire sort, red-black sort, splaysort, Dobosiewicz sort, psort, ...

external sorts.

• Poly-phase mergesort, cascade-merge, oscillating sort.

radix sorts.

• Distribution, MSD, LSD.
• 3-way radix quicksort.

parallel sorts.

• Bitonic sort, Batcher even-odd sort.
• Smooth sort, cube sort, column sort.
• GPUsort.

1. Bubble Sort

         exchange the neighbor nodes
  1. void bubbleSort(Comparable<Object>[] a):
  2. int k = a.length;
  3. boolean flag = true;
  4. while(flag):
  5. flag = false;
  6. for(int i=0;i<k-1;k++):
  7. if(a[i]>a[i+1]):
  8. int tmp = a[i];
  9. a[i] = a[i+1];
  10. a[i+1] = tmp;
  11. flag = true;
  12. k--;

2. Insertion Sort

        insert a[i] into a[0] - a[i-1]
  1. for(int i=0;i<a.length;i++):
  2. for(int j=i;j>0;j--):
  3. if(a[j]<a[j-1])
  4. int tmp = a[j-1];
  5. a[j-1] = a[j];
  6. a[j] = tmp;

3. Selection Sort

        select the minimal number from lowest index and exchange to current index
  1. for(int i=0;i<a.length;i++)
  2. int minIndex = i; // be aware! the index not the value;
  3. for(int j = i+1;j<a.length;j++)
  4. if(a[j]<a[min])
  5. min = j;
  6. //exchange min & i
  7. int tmp = a[i];
  8. a[i] = a[min];
  9. a[min] = a[i];

4. Merge Sort

  1. // extra space
  2. int[] aux;
  3. // separate array into two parts each time
  4. sort(int[] a, int lo, int hi):
  5. if(lo>hi)
  6. return;
  7. int mid = lo+(hi-lo)/2;
  8. sort(a,lo,mid);
  9. sort(a.mid+1,hi);
  10. merge(a,lo,mid,hi);
  11. merge(int[] a, int lo, int mid; int hi);
  12. int i = lo, j = mid;
  13. for(int k = lo; i<hi;k++):
  14. aux[k] = a[k];
  15. for(int k = lo; i<hi;k++):
  16. if(i>mid)
  17. a[k] = aux[j++];
  18. else if(j>hi)
  19. a[k] = aux[i++];
  20. else if(aux[j]>aux[i])
  21. a[k] = aux[i++];
  22. else
  23. a[k] = aux[j++];

5. Quick Sort

        // partition
        // recursion sort
  1. sort(int[] a, int lo, int hi):
  2. if(lo>hi):
  3. return;
  4. int j = partition(a,lo,hi);
  5. sort(a,lo,j-1);
  6. sort(a,j+1,hi);
  7. partition(int[] a, int lo, int hi):
  8. int i = lo, j = hi+1;
  9. int v = a[lo]; // pivot
  10. while(true):
  11. // 左右交换向前 向后
  12. // 三种条件 跳出循环!
  13. while(a[++i]<v):
  14. if(i==hi): break;
  15. while(a[--j]>v):
  16. if(j==lo): break;
  17. if(i>=j): break;
  18. // 交换 i j点
  19. exch(a,i,j);
  20. exch(a,lo,j);
  21. return j; //切分点

6. Shell Sort

  1. int N = a.length;
  2. int h = 1;
  3. while(h<N/3): // shell from large to small
  4. h = h * 3 + 1;
  5. while(h>=1):
  6. for(int i=h; i<N; i+=h):
  7. if(a[i]<a[i-h]):
  8. int tmp = a[i];
  9. int j = i-h;
  10. while(j>=0&&a[j]>tmp):
  11. a[j+h] = a[j];
  12. j-=h;
  13. a[j+h] = tmp;
  14. h = (h-1)/3;

7. Conclusion

Shellsort.
• Warmup: easy way to break the N2 barrier.
• Embedded systems.

Mergesort.
• Java sort for objects.
• Perl, Python stable sort.

Quicksort.
• Java sort for primitive types.
• C qsort, Unix, g++, Visual C++, Python.

Cracking Code Interview:

9.1 Merge two sorted two array: A is larger than B

  1. void merge(int a[], int b[], int n, int m):
  2. int i = n - 1; // for A last index
  3. int j = m - 1; // for B last index
  4. int k = n + m - 1; // for total index on A
  5. while(i>=0&&j>=0):
  6. if(a[i]>b[j]):
  7. a[k--] = a[i--];
  8. else
  9. a[k--] = b[j--];
  10. while(j>=0); // notice only b can remain the elements which need to be placed into A
  11. a[k--] = b[j--];

9.2 Sort an array of strings

  1. class AnagramComparator implements Comparator<String>:
  2. String sortChars(String s):
  3. char[] content = s.toCharArray();
  4. Arrays.sort(content);
  5. return new String(content);
  6. int compare(String s1, String s2):
  7. return sortChars(s1).compareTo(sortChars(s2));
  8. main:
  9. String[] strings -> Arrays.sort(array, new AnagramComparator());

9.4 think about >2GB file to sort

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注