Java Sorting Algorithm for code-insert sorting

Source: Internet
Author: User

Java Sorting Algorithm for code-insert sorting
  1. /**
  2. * The Java sorting algorithm implements code-insert 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. Print ("" + A [I] + "");
  15. System. Out. println ("");
  16. Insertsort (index-1); // select sorting
  17. // Sorted result
  18. System. Out. Print ("sorted :");
  19. For (I = 0; I <index-1; I ++)
  20. System. Out. Print ("" + A [I] + "");
  21. System. Out. println ("");
  22. }
  23. Public static void insertsort (INT index ){
  24. Int I, j, k; // cyclic count variable
  25. Int insertnode; // The data variable to be inserted.
  26. For (I = 1; I <index; I ++) // insert values in sequence
  27. {
  28. Insertnode = A [I]; // set the value to be inserted
  29. J = I-1; // the start position of the array to be inserted
  30. // Find the appropriate Insert Location
  31. While (j> = 0 & insertnode <A [J]) {
  32. A [J + 1] = A [J];
  33. J --;
  34. }
  35. A [J + 1] = insertnode; // insert a value
  36. // Print the current sorting result
  37. System. Out. Print ("sorting :");
  38. For (k = 0; k <index; k ++)
  39. System. Out. Print ("" + A [k] + "");
  40. System. Out. println ("");
  41. }
  42. }
  43. }
/*** Java sorting algorithm implements code-insert 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. print ("" + A [I] + ""); system. out. println (""); insertsort (index-1); // select the sorting result system. out. print ("sorted:"); for (I = 0; I <index-1; I ++) system. out. print ("" + A [I] + ""); system. out. println ("");} public static void insertsort (INT index) {int I, j, k; // The cyclic count variable int insertnode; // to insert the data variable for (I = 1; I <index; I ++) // Insert the value in sequence {insertnode = A [I]; // set the value J = I-1; // start position of the array to be inserted // find the appropriate insert position while (j> = 0 & insertnode <A [J]) {A [J + 1] = A [J]; j --;} A [J + 1] = insertnode; // Insert the value into the system. // print the current sorting result. out. print ("in sorting:"); For (k = 0; k <index; k ++) system. out. print ("" + A [k] + ""); system. out. println ("");}}}

Running result

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

Sorting: 10 32 1 9 5 7 12 0 4

Sorting: 1 10 32 9 5 7 12 0 4

Sorting: 1 9 10 32 5 7 12 0 4

Sorting: 1 5 9 10 32 7 12 0 4

Sorting: 1 5 7 9 10 32 12 0 4

Sorting: 1 5 7 9 10 12 32 0 4

Sorting: 0 1 5 7 9 10 12 32 4

Sorting: 0 1 4 5 7 9 10 12 32

After sorting: 0 1 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.