Java Sorting Algorithm Implementation Code-select-type sorting

Source: Internet
Author: User

Java Sorting Algorithm Implementation Code-select-type sorting
  1. /**
  2. * The Java sorting algorithm implements code-select-based sorting.
  3. *
  4. * @ Author old zizhu Java century network (java2000.net)
  5. *
  6. */
  7. Public class test {
  8. Public static int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // default data array
  9. Public static void main (string ARGs []) {
  10. Int I; // The variable of the cyclic count.
  11. Int Index = A. length; // data index variable
  12. System. Out. Print ("Before sorting :");
  13. For (I = 0; I <index-1; I ++)
  14. System. Out. printf ("% 3 s", a [I]);
  15. System. Out. println ("");
  16. Selectsort (index-1); // select sorting
  17. // Sorted result
  18. System. Out. Print ("sorted :");
  19. For (I = 0; I <index-1; I ++)
  20. System. Out. printf ("% 3 s", a [I]);
  21. System. Out. println ("");
  22. }
  23. Public static void selectsort (INT index ){
  24. Int I, j, k; // cyclic count variable
  25. Int minvalue; // minimum variable
  26. Int indexmin; // minimum index variable
  27. Int temp; // temporary variables
  28. For (I = 0; I <index-1; I ++ ){
  29. Minvalue = 32767; // current minimum value
  30. Indexmin = 0; // store the index value of the minimum value
  31. For (j = I; j <index; j ++ ){
  32. If (A [J] <minvalue) // locate the minimum value
  33. {
  34. Minvalue = A [J]; // minimum value for storage
  35. Indexmin = J;
  36. }
  37. Temp = A [I]; // exchanges two values.
  38. A [I] = A [indexmin];
  39. A [indexmin] = temp;
  40. }
  41. System. Out. Print ("sorting :");
  42. For (k = 0; k <index; k ++)
  43. System. Out. printf ("% 3 s", a [k]);
  44. System. Out. println ("");
  45. }
  46. }
  47. }
/*** Java sorting algorithm implements code-select-based sorting. ** @ Author old zizhu Java century network (java2000.net) **/public class test {public static int [] A = {10, 32, 1, 9, 5, 7, 12, 0, 4, 3}; // default data array public static void main (string ARGs []) {int I; // The cyclic count variable int Index =. length; // The data index variable system. out. print ("Before sorting:"); for (I = 0; I <index-1; I ++) system. out. printf ("% 3 s", a [I]); system. out. println (""); selectsort (index-1); // select the sorting result system. out. print ("sorted:"); for (I = 0; I <index-1; I ++) system. out. printf ("% 3 s", a [I]); system. out. println ("");} public static void selectsort (INT index) {int I, j, k; // The Int minvalue variable of the cyclic count; // The Int indexmin variable of the minimum value; // minimum index variable int temp; // temporary variable for (I = 0; I <index-1; I ++) {minvalue = 32767; // currently, the minimum value indexmin = 0; // store the index value of the minimum value for (j = I; j <index; j ++) {if (a [J] <minvalue) // find the minimum value {minvalue = A [J]; // store the minimum value indexmin = J;} temp = A [I]; // exchange two values a [I] = A [indexmin]; A [indexmin] = temp;} system. out. print ("in sorting:"); For (k = 0; k <index; k ++) system. out. printf ("% 3 s", a [k]); system. out. println ("");}}}

Running result

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

Sorting: 1 32 10 9 5 7 12 0 4

Sorting: 1 5 32 10 9 7 12 0 4

Sorting: 1 5 9 32 10 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

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

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.