By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

Need help on quick sort - v.Urgent

needhelp123
P: 21
Can any one send me a quick sort simple logic pogram...

its very urgent urgent
May 22 '07 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
Can any one send me a quick sort simple logic pogram...

its very urgent urgent
Why don't you try to write one yourself? Do you know the algorithm?
May 22 '07 #2

needhelp123
P: 21
Why don't you try to write one yourself? Do you know the algorithm?
Expand|Select|Wrap|Line Numbers
  1.  public class QuickSort { 
  2. int i; // Array index
  3.  
  4. int j; // Array index
  5.  
  6. /**
  7. * @precondition Unsorted elements
  8. * @postcondition Sorted elements
  9. * @param qsArrays
  10. * @param len
  11. */
  12. public void setQuickSort(int qsArrays[], int len) {
  13. setQuicksort(qsArrays, 0, len - 1);
  14. }
  15.  
  16. private void setQuicksort(int qsArrays[], int left, int right) {
  17. int middle = left;
  18.  
  19. //block where array division starts
  20. swap(qsArrays, left, (left + right) / 2);
  21. for (int i = left + 1; i <= right; i++) {
  22. if (qsArrays[i] <= qsArrays[left]) {
  23. middle++;
  24. //recursive function for swap
  25. swap(qsArrays, i, middle);
  26. }
  27. }
  28.  
  29. //recursive method o divide array into its child
  30. swap(qsArrays, left, middle);
  31.  
  32. // Blockfor soerting arrays after the list is divided into sub array
  33. if (left + 1 < middle) {
  34. setQuicksort(qsArrays, left, middle - 1);
  35. }
  36.  
  37. if (middle + 1 < right) {
  38. setQuicksort(qsArrays, middle + 1, right);
  39. }
  40. } // end setQuicksort
  41.  
  42. /**
  43. * Swap numbers
  44. * @param list
  45. * @param a
  46. * @param b
  47. */
  48. private void swap(int list[], int a, int b) {
  49. int temp = list[a]; //temp holds first number
  50. list[a] = list[b]; //first index holds a value second index
  51. list[b] = temp; // second index value holds a value of temp variable 
  52. }
  53.  
  54. /**
  55. * The main method illustrates the use of a quick sort to sort a 
  56. * small array with 5 values
  57. * @precondition: (None)
  58. * @Postcondition: Sorted Array
  59. */
  60. public static void main(String str[]) {
  61. //array Initialization
  62. int[] qsArrays = new int[5];
  63.  
  64. //insert elements into array
  65. qsArrays[0] = 20;
  66. qsArrays[1] = 10;
  67. qsArrays[2] = 50;
  68. qsArrays[3] = 100;
  69. qsArrays[4] = 05;
  70.  
  71. // Print the array before sorting:
  72. System.out.println("Data before Soring ");
  73. System.out.println("**************** ");
  74.  
  75. //block to print unsorted elements
  76. for (int val = 0; val < 5; val++) {
  77. System.out.println("Value at index" + val + " = " + qsArrays[val]);
  78. }
  79.  
  80. QuickSort acc = new QuickSort();
  81. acc.setQuickSort(qsArrays, 5); //calling setQuickSort method
  82.  
  83. System.out.println(" ");
  84. System.out.println(" ");
  85. System.out.println("Data After Soring");
  86. System.out.println("****************");
  87.  
  88. //block to print sorted elements
  89. for (int val = 0; val < 5; val++) {
  90. System.out.println("Value at index" + val + " = " + qsArrays[val]);
  91. }
  92. }
  93. }
  94.  
this is complex... i need simple.....
May 22 '07 #3

prometheuzz
Expert 100+
P: 197
...

this is complex... i need simple.....
Here ya go:

Expand|Select|Wrap|Line Numbers
  1. public class ReallyQuickSort {
  2.  
  3.     static void display(String message, int[] array) {
  4.         System.out.print(message);
  5.         for(int i = 0; i < array.length; i++) {
  6.             System.out.print(array[i]+" ");
  7.         }
  8.     }
  9.  
  10.     static void sort(int[] array) {
  11.         java.util.Arrays.sort(array);
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         int[] array = {20, 10, 50, 100, 5};
  16.         display("Before sorting: ", array);
  17.         sort(array);
  18.         display("\nAfter sorting:  ", array);
  19.     }
  20. }
May 22 '07 #4

Expert 10K+
P: 11,448
this is complex... i need simple.....
That quicksort implementation is as simple as they come. Don't just try to scrape
"TeH CoDeZ!!!11eleven UGRENT" from the internet; think for yourself instead.

kind regards,

Jos
May 22 '07 #5

needhelp123
P: 21
That quicksort implementation is as simple as they come. Don't just try to scrape
"TeH CoDeZ!!!11eleven UGRENT" from the internet; think for yourself instead.

kind regards,

Jos
do not try to put down any one if u know the concept let me.. kno that program is designed by me.....
thank you
May 22 '07 #6

needhelp123
P: 21
Here ya go:

Expand|Select|Wrap|Line Numbers
  1. public class ReallyQuickSort {
  2.  
  3.     static void display(String message, int[] array) {
  4.         System.out.print(message);
  5.         for(int i = 0; i < array.length; i++) {
  6.             System.out.print(array[i]+" ");
  7.         }
  8.     }
  9.  
  10.     static void sort(int[] array) {
  11.         java.util.Arrays.sort(array);
  12.     }
  13.  
  14.     public static void main(String[] args) {
  15.         int[] array = {20, 10, 50, 100, 5};
  16.         display("Before sorting: ", array);
  17.         sort(array);
  18.         display("\nAfter sorting:  ", array);
  19.     }
  20. }
promo,
before itself i couild have used tahat redimade util package,,,,

i asked would i be possible for you to update that,,, i mean simler by using the concep what i have,...
May 22 '07 #7

prometheuzz
Expert 100+
P: 197
promo,
before itself i couild have used tahat redimade util package,,,,

i asked would i be possible for you to update that,,, i mean simler by using the concep what i have,...
No, I'm done helping you.
May 22 '07 #8

needhelp123
P: 21
No, I'm done helping you.

ok promo.... i am workin ion that sort to make it simple will be complet it by evening
May 22 '07 #9

10K+
P: 13,264
ok promo.... i am workin ion that sort to make it simple will be complet it by evening
Please tell us how it goes.
May 23 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.