Java Common algorithms

Source: Internet
Author: User

Ext.: http://blog.csdn.net/spy19881201/article/details/5867721

Https://www.cnblogs.com/hexiaochun/archive/2012/09/03/2668324.html

First, bubble sort

[Java]View PlainCopy
  1. Package sort.bubble;
  2. Import Java.util.Random;
  3. /**
  4. * Compare adjacent two numbers in turn, place decimals in front, large numbers on the back
  5. * Bubble sort, with stability
  6. * Time complexity of O (n^2)
  7. * Less than heap sort, quick sort O (Nlogn, base 2)
  8. * @author Liangge
  9. *
  10. */
  11. Public class Main {
  12. public static void Main (string[] args) {
  13. Random ran = new Random ();
  14. int[] sort = new int[10];
  15. For (int i = 0; i < i++) {
  16. Sort[i] = Ran.nextint (50);
  17. }
  18. System.out.print ("Array before sorting is");
  19. For (int i:sort) {
  20. System.out.print (i+"");
  21. }
  22. Buddlesort (sort);
  23. System.out.println ();
  24. System.out.print ("sorted array is");
  25. For (int i:sort) {
  26. System.out.print (i+"");
  27. }
  28. }
  29. /** 
  30. * Bubble Sort
  31. * @param sort
  32. */
  33. private static void Buddlesort (int[] sort) {
  34. For (int i=1;i<sort.length;i++) {
  35. For (int j=0;j<sort.length-i;j++) {
  36. if (sort[j]>sort[j+1]) {
  37. int temp = sort[j+1];
  38. sort[j+1] = sort[j];
  39. SORT[J] = temp;
  40. }
  41. }
  42. }
  43. }
  44. }

Second, choose the sort

[Java]View PlainCopy
  1. Package sort.select;
  2. Import Java.util.Random;
  3. /**
  4. * Select sort
  5. * Each trip selects the smallest (or largest) element from the data element to be sorted,
  6. * The order is placed at the end of the ordered sequence, until all the data elements are sorted out.
  7. * Select Sort is an unstable sort method.
  8. * @author Liangge
  9. *
  10. */
  11. Public class Main {
  12. public static void Main (string[] args) {
  13. Random ran = new Random ();
  14. int[] sort = new int[10];
  15. For (int i = 0; i < i++) {
  16. Sort[i] = Ran.nextint (50);
  17. }
  18. System.out.print ("Array before sorting is");
  19. For (int i:sort) {
  20. System.out.print (i + "");
  21. }
  22. Selectsort (sort);
  23. System.out.println ();
  24. System.out.print ("sorted array is");
  25. For (int i:sort) {
  26. System.out.print (i + "");
  27. }
  28. }
  29. /** 
  30. * Select sort
  31. * @param sort
  32. */
  33. private static void Selectsort (int[] sort) {
  34. For (int i =0;i<sort.length-1;i++) {
  35. For (int j = i+1;j<sort.length;j++) {
  36. if (Sort[j]<sort[i]) {
  37. int temp = sort[j];
  38. SORT[J] = Sort[i];
  39. Sort[i] = temp;
  40. }
  41. }
  42. }
  43. }
  44. }

Three, quick sort

 1 package Com.quick; 2 3 public class Quick {4 public void Quick_sort (int[] arrays, int lenght) {5 if (null = = Arrays | | lenght < 1) {6 System.out.println ("Input error!"); 7 return; 8} 9 _quick_sort (arrays , 0, lenght-1);}11 public void _quick_sort (int[] arrays, int start, int end) {if (start>=end) {return;15}16-int i = start;18 int j = end;19 int value = array  S[I];20 Boolean flag = true;21 while (i! = j) {if (flag) {(Value >                     Arrays[j]) {Swap (arrays, I, j); flag=false;26} else {28                     j--;29}30}else{31 if (Value<arrays[i]) {32          Swap (arrays, I, j); Flag=true;34}else{35 i++;36       }37}38}39 SNP (arrays); _quick_sort (arrays, start, j-1); _quick_so RT (Arrays, i+1, end),}44, public void SNP (int[] arrays) {$ = (int i = 0; i < arrays. Length i++) {System.out.print (Arrays[i] + "),}49 System.out.println (),}51 (Priva) te void Swap (int[] arrays, int i, int j) {int temp;54 temp = arrays[i];55 Arrays[i] = arrays[j]          ; Arrays[j] = temp;57}58 public static void Main (String args[]) {Quick q = new quick (); 61 Int[] A = {$, 65,12,45,5,};62 q.quick_sort (a,6); 63} 64 65}

Quick Sort Explanation

The idea of the so-called quick sort is to first take out the first number of the array as a key, set a i,j as the identity before and after each, and then take the key to the array from behind, and j--, until we find the first number less than this key, and then exchange the two values, after the exchange is complete , we take this key to go backwards from I, and i++, loop until the end of the i=j, when this is over, we will find that the value greater than this key will go to the back of the key, not if you write the wrong, less than this key will run to the front of this value Then we can complete the ordering of the entire array by invoking the array recursively.

Represented by a graphical representation of the following:

This will be divided into two segments of the key, we will take these two paragraphs further to solve the problem.

Java Common algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.