Java Sorting Algorithm Implementation Code-shell sorting

Source: Internet
Author: User

Java Sorting Algorithm Implementation Code-shell sorting
  1. /**
  2. * The Java sorting algorithm implements code-shell 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. Shellsort (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 shellsort (INT index ){
  24. Int I, j, k; // cyclic count variable
  25. Int temp; // temporary variables
  26. Boolean change; // whether the data is changed
  27. Int datalength; // Interval Length of the split set
  28. Int pointer; // The processing position.
  29. Datalength = (INT) index/2; // The Interval Length of the initial set.
  30. While (datalength! = 0) // The sequence can still be split
  31. {
  32. // Process each set
  33. For (j = datalength; j <index; j ++ ){
  34. Change = false;
  35. Temp = A [J]; // Save the value of data [J], which is used for exchanging values
  36. Pointer = J-datalength; // the location where the computation is processed
  37. // Compare and exchange values in the Set
  38. While (temp <A [pointer] & pointer> = 0 & pointer <= index ){
  39. A [pointer + datalength] = A [pointer];
  40. // Calculate the next location for processing
  41. Pointer = pointer-datalength;
  42. Change = true;
  43. If (pointer <0 | pointer> index)
  44. Break;
  45. }
  46. // Exchange with the final value
  47. A [pointer + datalength] = temp;
  48. If (Change ){
  49. // Print the current sorting result
  50. System. Out. Print ("sorting :");
  51. For (k = 0; k <index; k ++)
  52. System. Out. printf ("% 3 s", a [k]);
  53. System. Out. println ("");
  54. }
  55. }
  56. Datalength = datalength/2; // calculate the Interval Length of the next split
  57. }
  58. }
  59. }
/*** Java sorting algorithm implements code-shell 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 (""); shellsort (index-1); // select the sorting result system. out. print ("after sorting: "); For (I = 0; I <index-1; I ++) system. out. printf ("% 3 s", a [I]); system. out. println ("");} public static void shellsort (INT index) {int I, j, k; // The cyclic count variable int temp; // The temporary variable Boolean change; // whether the data changes int datalength; // whether the Interval Length of the split set is int pointer; // The processing position datalength = (INT) index/2; // The Interval Length of the initial set while (datalength! = 0) // The sequence can still be split {// process each set for (j = datalength; j <index; j ++) {change = false; temp = A [J]; // Save the value of data [J]. Use Pointer = J-datalength to exchange the value; // calculate the processing position // compare the values in the set with the exchange value while (temp <A [pointer] & pointer> = 0 & pointer <= index) {A [pointer + datalength] = A [pointer]; // calculate the next vertex to be processed. pointer = pointer-datalength; change = true; if (pointer <0 | pointer> index) break;} // exchange a [pointer + datalength] = temp with the final value; If (change) {// print the current sorting result system. out. print ("in sorting:"); For (k = 0; k <index; k ++) system. out. printf ("% 3 s", a [k]); system. out. println ("") ;}} datalength = datalength/2; // calculate the Interval Length of the next split }}}

Running result

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

Sorting: 5 32 1 9 10 7 12 0 4

Sorting: 5 7 1 9 10 32 12 0 4

Sorting: 5 7 1 0 10 32 12 9 4

Sorting: 4 7 1 0 5 32 12 9 10

Sorting: 1 7 4 0 5 32 12 9 10

Sorting: 1 0 4 7 5 32 12 9 10

Sorting: 1 0 4 7 5 9 12 32 10

Sorting: 1 0 4 7 5 9 10 32 12

Sorting: 0 1 4 7 5 9 10 32 12

Sorting: 0 1 4 5 7 9 10 32 12

Sorting: 0 1 4 5 7 9 10 12 32

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

Related Article

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.