Java

advertisement
By
Jonathan Villanueva
 Bubble Sorting: the way to sort an array by switching two
values that are right next to each other if the first number
bigger than the second.
 Insertion Sorting: Takes a value from an input index and
places the value in the correct place in an array one at a time.
 Shell Sorting: Similar to insertion sort, but instead of going
one by one in the index, the shell sort is able to take larger
steps in order to find where the value belongs
 Quick Sorting: Divide the list into two categories, and if
either list is higher or lower than the value, that part of the
list is erased from the process, and this process is continued
until the appropriate placement of the value is found.
 As mentioned before, bubble sorting is the process of
going through an array using a do while loop and if
one of the values at array[x+1] < array[x], then the two
numbers are switched and the loop will go again.
 Process:
-1 22 11 14
-1 11 22 14
-1 11 14 22
 Efficient on smaller indexes of data
 simple to understand
 Slower than other methods of sorting, especially on
larger indexes of data(due to the multiple runs and
switching one by one).
 Not used as much due to improved and quicker
methods of sorting
















boolean cont=true;
do //do while loop
{
cont=false; //set boolean to false that way the loop will stop if nothing changes
for (int x=0; x<array.length-2; x++) //for each number in the array besides the very last number
{
if(array[x]>array[x+1]) // if the number in front is bigger than the second number addressed
{
int temp=array[x]; //switch the two
array[x]=array[x+1];
array[x+1]=temp;
cont=true; //run the loop again
}
}
}
while(cont=true);
 Essentially, one can think of insertion sorting as having a list of
randomly arranged words and putting them down on a page in
alphabetical order by simply putting them on the page one at a
time starting from the top.
 Process: data ->Bill, Jeff, Alison
-Bill
-Bill, Jeff (since “J” is after “B”, Jeff does not go in front of Bill,
so it must go behind)
-Alison ,Bill , Jeff
*Note: as the program is running this, it is running through your
array you’re ordering your values and seeing if array[x]> the given
value. If it is, then the program goes to the next value in the
array, or array[x+1]
 Simple
 Efficient on small indexes of data
 More efficient than bubble sort
 Slower than many forms of sorting when it comes to
larger sets of data and lists of data that are not already
somewhat in order.
 It does not work well for reversely sorted arrays











for (int x= 1; x< array.length; x++) //loop for the length of the array
{
int store= array[x]; //store the value at x
int y= x-1;
while ((y>= 0) && array[y] > store) //while the array[x1]>array[x]…
{
array[y+1] = array[y]; //move the value at array[x-1] up one
value
y--; //decrease y
}
array[y+1] = store; //place the value at the index array[x]
}
 Shell sort is the same as insertion sort except it
compares elements separated by a gap of several
positions. As the loop continues, the gap shrinks
 Efficient
 Faster than Insertion sort (due to the large gaps the
shell sorting uses)
 More complex and longer than other methods


















int stop,swap,limit,temp;
int x=(int)(max/2)-1;
while(x>0)
{
stop=0;
limit=max-x;
while(stop==0)
{
swap=0;
for(int k=0;k<limit;k++)
{
if(A[k]>A[k+x])
{
temp=A[k];
A[k]=A[k+x];
A[k+x]=temp;
swap=k;

}

}

limit=swap-x;

if(swap==0)

stop=1;

}

x=(int)(x/2);
}


}


int main()

{

int i;

int X[ELEMENTS]={5,2,4,6,1,3};

printf("Unsorted Array:\n");

for(i=0;i<ELEMENTS;i++)

printf("%d ",X[i]);


shellsort(X,ELEMENTS);

printf("\nSORTED ARRAY\n");

for(i=0;i<ELEMENTS;i++)


printf("%d ",X[i]);
 Basically this type of sorting follows the same rules as
the higher-lower game with the strategy of dividing
the length of the index of data in half and finding the
value at the middle of the array. If the data is higher
than the halfway point, then you disregard the lower
half and vice versa. The process continues until you
find the place where the value belongs. In other words,
a perfect chance for recursion.
 Fastest of the sorting codes when it comes to longer
code.
 Shorter code than the shell method
 Efficient
 When the data is in order

int lo = low;
int hi = n;
if (lo >= n) {
return;
}
int mid = array[(lo + hi) / 2];
while (lo < hi) {
while (lo<hi && array[lo] < mid) {
lo++;
}
while (lo<hi && array[hi] > mid) {
hi--;
}
if (lo < hi) {
int T = array[lo];
array[lo] = array[hi];
array[hi] = T;
}
}
if (hi < lo) {
int T = hi;
hi = lo;
lo = T;
}
quick_srt(array, low, lo);
quick_srt(array, lo == low ? lo+1 : lo, n);
 http://www.java-tips.org/java-se-tips/java.lang/insertion-sort




implementation-in-java.html
http://www.java-tips.org/java-se-tips/java.lang/shell-sortimplementation-in-java.html
http://www.java-tips.org/java-se-tips/java.lang/quick-sortimplementation-with-median-of-three-partitioning-and-cutoff-forsmall-a.html
http://www.softpanorama.org/Algorithms/Sorting/insertion_sort.sht
ml
http://www.dreamincode.net/code/snippet281.htm
http://www.roseindia.net/java/beginners/arrayexamples/QuickSort.sh
tml
Download