Java Sorting Algorithm for code-Quick Sort

Source: Internet
Author: User

Java Sorting Algorithm Implementation Code-bubble sort sorting
  1. /**
  2. * The Java sorting algorithm implements the code-bubble sort sorting.
  3. *
  4. * @ Author old zizhu Java century network (java2000.net)
  5. *
  6. */
  7. Public class test {
  8. Public static void main (string [] ARGs ){
  9. Int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };
  10. System. Out. Print ("Before sorting :");
  11. For (INT I = 0; I <A. length; I ++)
  12. System. Out. printf ("% 3 s", a [I]);
  13. System. Out. println ();
  14. Test test = new test ();
  15. Test. bubblesort ();
  16. System. Out. Print ("sorted :");
  17. For (INT I = 0; I <A. length; I ++)
  18. System. Out. printf ("% 3 s", a [I]);
  19. System. Out. println ();
  20. }
  21. Public void bubblesort (INT [] ){
  22. Int Len = A. length;
  23. System. Out. println ("the array size is:" + Len );
  24. Boolean change = false;
  25. Int temp;
  26. Int COUNT = 0;
  27. For (INT I = Len; I> 1; I --){
  28. For (Int J = 0; j <I-1; j ++ ){
  29. If (A [J + 1] <A [J]) {
  30. Temp = A [J + 1];
  31. A [J + 1] = A [J];
  32. A [J] = temp;
  33. Change = true;
  34. Count ++;
  35. }
  36. }
  37. If (Change ){
  38. System. Out. Print ("th" + Count + ":");
  39. For (int K = 0; k <Len; k ++)
  40. System. Out. Print (A [k] + "");
  41. System. Out. println ();
  42. }
  43. }
  44. }
  45. }
/*** Java sorting algorithm implements code-Bubble sorting. ** @ Author old zizhu Java century network (java2000.net) **/public class test {public static void main (string [] ARGs) {int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; system. out. print ("Before sorting:"); For (INT I = 0; I <. length; I ++) system. out. printf ("% 3 s", a [I]); system. out. println (); test = new test (); test. bubblesort (a); system. out. print ("sorted:"); For (INT I = 0; I <. length; I ++) system. out. printf ("% 3 s", a [I]); system. out. println ();} public void bubblesort (INT [] A) {int Len =. length; system. out. println ("the array size is:" + Len); Boolean change = false; int temp; int COUNT = 0; For (INT I = Len; I> 1; I --) {for (Int J = 0; j <I-1; j ++) {if (a [J + 1] <A [J]) {temp = A [J + 1]; A [J + 1] = A [J]; A [J] = temp; change = true; count ++ ;}} if (Change) {system. out. print ("th" + Count + ":"); For (int K = 0; k <Len; k ++) system. out. print (A [k] + ""); system. out. println ();}}}}

Running result

Before sorting: 10 32 1 9 5 7 12 0 4 3

The array size is 10.

8th trip switching: 10 1 9 5 7 12 0 4 3 32

15th exchange: 1 9 5 7 10 0 4 3 12 32

20th trip exchange: 1 5 7 9 0 4 3 10 12 32

23rd trip exchange: 1 5 7 0 4 3 9 10 12 32

26th trip exchange: 1 5 0 4 3 7 9 10 12 32

29th trip switching: 1 0 4 3 5 7 9 10 12 32

31st trip switching: 0 1 3 4 5 7 9 10 12 32

31st trip switching: 0 1 3 4 5 7 9 10 12 32

31st trip switching: 0 1 3 4 5 7 9 10 12 32

After sorting: 0 1 3 4 5 7 9 10 12 32

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.