Documente Academic
Documente Profesional
Documente Cultură
In this example we are going to sort integer values of an array using quick sort.
Quick sort algorithm is developed by C. A. R. Hoare. Quick sort is a comparison sort. The
working of quick sort algorithm is depending on a divide-and-conquer strategy. A divide and
conquer strategy is dividing an array into two sub-arrays. Quick sort is one of the fastest and
simplest sorting algorithm. The complexity of quick sort in the average case is Θ(n log(n))
and in the worst case is Θ(n2).
Code description:
In quick sort algorithm pick an element from array of elements. This element is called the
pivot. Then compare the the values from left to right until a greater element is find then swap
the values. Again start comparison from right with pivot. When lesser element is find then
swap the values. Follow the same steps until all elements which are less than the pivot come
before the pivot and all elements greater than the pivot come after it. After this partitioning,
the pivot is in its last position. This is called the partition operation. Recursively sort the sub-
array of lesser elements and the sub-array of greater elements.
System.out.println("\n\n RoseIndia\n\n");
System.out.println(" Quick Sort\n\n");
System.out.println("Values Before the sort:\n");
for(i = 0; i < array.length; i++)
System.out.print( array[i]+" ");
System.out.println();
quick_srt(array,0,array.length-1);
System.out.print("Values after the sort:\n");
for(i = 0; i <array.length; i++)
System.out.print(array[i]+" ");
System.out.println();
System.out.println("PAUSE");
}
Introduction
In this example we are going to sort integer values of an array using merge sort.
In merge sorting algorithm unsorted values are divided into two equal parts iteratively. Then
merge both parts and sort it. Then again merge the next part and sort it. Do it iteratively until
the values are not in sorted order. In merge sorting the number of elements must be even. The
merge sorting is invented by John von Neumann in 1945 .
The complexity of the merge sorting is in worst-case O(n log n) and in average case O(n log
n).
Code description:
In merge sort split the array values in halves recursively until each half has only single
element. Merge the two 1/2 values together and sort the values. Do same steps iteratively until
the values are not sorted.
Top of Form
partner-pub-0714 FORID:10
Search
Search
Bottom of Form
Discuss: Bubble Sorting in Java In this example we are going to sort integer values of an
array using bubble sort..
Introduction
In this example we are going to sort integer values of an array using bubble sort.
Bubble sort is also known as exchange sort. Bubble sort is a simplest sorting algorithm. In
bubble sort algorithm array is traversed from 0 to the length-1 index of the array and
compared one element to the next element and swap values in between if the next element
is less than the previous element. In other words, bubble sorting algorithm compare two
values and put the largest value at largest index. The algorithm follow the same steps
repeatedly until the values of array is sorted. In worst-case the complexity of bubble sort is
O(n2) and in best-case the complexity of bubble sort is Ω(n).
Code description:
Bubble Sorting is an algorithm in which we are comparing first two values and put the
larger one at higher index. Then we take next two values compare these values and place
larger value at higher index. This process do iteratively until the largest value is not reached
at last index. Then start again from zero index up to n-1 index. The algorithm follows the
same steps iteratively unlit elements are not sorted.
Compare these two values .As 12>4 then we have to swap these values.
Then the new sequence will be
9 4 12 99 120 1 3 10