Java Simple Sort __java

Source: Internet
Author: User
Tags int size

First, bubble sort


As the figure shows, if there are five elements that need to be sorted, then you need to row n-1, so we can write a loop out,

for (int i = 1; I <= size-1; i++)
But we found that each time the number of comparisons decreases as the number of trips increases, so can we change the loop?

for (int i = size-1 i > 0; i--)
So it's not as if I'm decreasing as the number of trips increases, now that we have written the framework of the algorithm, we have to compare elements, there are graphs on the analysis that the comparison of the number of times to reduce, and are all from the subscript 0 start comparison, so, also need a loop to compare elements

for (Int J =0; J <?; J + +)

At this point, the question is, what is the critical value, ". "is a few, and then analysis, the first trip to compare the subscript from 0 to 4, the second comparison subscript from 0 to 3 to find the change of the small sign and I is the same, and can equal I, so modify the loop

for (Int J =0 J <= I; j + +)
At this time feel very right, but think carefully, this for loop content is what, should be source[j] and source[j+1] to compare, and then Exchange, if J can equal I, then j+1 is not bigger than I, so we should take the equal sign out, change
for (Int J =0 J < I; J + +)
To make a name meaningful, you can modify the two loops

for (int outer = size-1, outer > 0; outer--)
{for
    (int inner = 0; inner < outer; inner++)
    {
	//comparison logic C4/>int temp = Source[inner + 1];
        Source[inner + 1] = Array[inner];
        Source[inner] = temp;
    }
}
The full program looks like this:

Package com.darren.test.sort;

public class Bubblesorttest {public
    static void Main (string[] args) {
        int[] array = new int[] {3, 5, 9, 8, 6, 4, 3, 1, 8, 7};
        
        System.out.print ("before sort:");
        print (array);
        System.out.println ();
        
        int size = Array.Length;
        for (int outer = size-1, outer >= 0; outer--) {for
            (int inner = 0; inner < outer; inner++) {
                if array[in NER] > Array[inner + 1]) {
                    int temp = Array[inner + 1];
                    Array[inner + 1] = Array[inner];
                    Array[inner] = temp;
                }
        
        }} System.out.print ("after sort:");
        print (array);

    private static void print (int[] source {for
        (Integer data:source) {
            System.out.print (data);
            System.out.print (",");}}


Second, insert sort


As the figure shows, if there are five elements that need sorting, then you need to row n-1, and the subscript is from 1 to n-1, then we can write a loop out

for (int i = 1; i < size; i++)
As we can see from the diagram, we are always source[i to the left, then should be source[i] temporary preservation, I think it should be

int temp = Source[i];
Then how the internal algorithm is compared to the insertion of the next analysis, we found that the left side is always inserted, in fact, the left side of the sequence is always orderly, then we will be inserted from right to left, so the second for loop is inverted
for (int j =?; J >=0; j--)
I can see from the figure that J is 0, then the maximum can be how much, and then analysis, the maximum than I small 1, so modify the For loop
for (int j = i-1 J >=0; j--)
How do you compare it?
SOURCE[J] and source[j+1] for comparison. At this time source[j+1] just equals source[i], because j + 1 = i;

To make a variable meaningful, the result is

for (int outer = 1; outer < size; outer++)
{
    int sentinel = source[i];
    for (int inner = outer-1; inner >= 0; inner--)
    {
        if (Source[inner] > Source[inner + 1])
        {
            Source[inn ER + 1] = Source[inner];
            Source[inner] = Sentinel;
        }
    }
The full program looks like this:

Package com.darren.test.sort;

public class Insertsorttest {public
    static void Main (string[] args) {
        int[] array = new int[] {3, 5, 9, 8, 6, 4, 3, 1, 8, 7};
        
        System.out.print ("before sort:");
        print (array);
        System.out.println ();
        
        int size = Array.Length;
        for (int i = 1; i < size; i++) {
            int sentinel = array[i];
            for (int j = i-1; Sentinel < array[j]; j--) {
                array[j + 1] = Array[j];
                ARRAY[J] = Sentinel;
                if (j = = 0) {break
                    ;
                }
        
        }} System.out.print ("after sort:");
        print (array);

    private static void print (int[] source {for
        (Integer data:source) {
            System.out.print (data);
            System.out.print (",");}}

Third, select the sort






As the figure shows, if there are five elements that need to be sorted, the macroscopic view needs to row n-1, and the subscript is from 0 to n-2 (note the beginning of the black underline), then we can write a loop out

for (int i = 0; i < size-1; i++)
The first for loop doesn't care exactly how to choose the smallest one to put to the left, just find the subscript change law, and then we see how the interior is to choose the smallest value to the left, the implementation of the relative insertion order is different, only with the circle is not easy to express, so we use a specific example to see.
Sorts an array of five elements: 3 1 5 2 4

To compare, there must be a reference, let's assume that the leftmost element in each trip (the leftmost element of the black underline) is the reference (int minvalueindex = i), and then an element (int j = i + 1) is compared to it, if smaller than it is, then change the references, Take the new smallest element as a reference, continue to compare the elements after it, until the end of this trip, because you want to take the elements of J and the left to compare without J + 1, so j is preferable to n-1
That

for (int j = i + 1; j < size; J + +)
Here are two questions.
1. Select an element as a reference or an element's subscript.
2. What should be done after each trip.

Let's first answer the second question, and each time we compare it, we should put the smallest element in the left position, and we need to do an element exchange.

And then answer the first question, and we know that we're going to exchange elements, if you use the element as a reference, then I only know the minimum is a few, but can not exchange with other elements, because I do not know the minimum value of the subscript, but if we know the minimum value of the subscript, we can know the minimum value, so we use subscript as a reference

for (int outer = 0; outer < size-1; outer++)
{
    //Set reference to the leftmost element
    int min = outer;
    for (int inner = outer + 1; inner < size; inner++)
    {
        if (Source[inner] < source[min])
        {
            //update reference
            m in = inner;
        }
    }

    Exchange minimum and leftmost value
    int temp = Source[outer];
    Source[outer] = source[min];
    Source[min] = temp;
}


The full program looks like this:

package Com.darren.test.sort; public class Selectsorttest {public static void main (string[] args) {int[] array = new int[] {3, 5, 9, 8, 6
        
        , 4, 3, 1, 8, 7};
        System.out.print ("before sort:");
        print (array);
        
        System.out.println ();
        int size = Array.Length;
            for (int outer = 0; outer < size-1; outer++) {int min = outer;
                    for (int inner = outer + 1; inner < size; inner++) {if (Array[inner] < array[min]) {
                min = inner;
            int temp = Array[outer];
            Array[outer] = Array[min];
        Array[min] = temp;
        } System.out.print ("After sort:");
    print (array); 
            private static void print (int[] source {for (Integer Data:source) {System.out.print (data);
        System.out.print (","); }
    }
}

Summarize:

Comparison of several simple sorting algorithms
In general, the bubble sort is not used, it is too simple, so that it can be effortlessly written out, when the data is very small use value.
The select sort, while minimizing the number of exchanges, is still a large number of comparisons, and is used when the data volume is small and the exchange data is more time-consuming than the comparison data.
In most cases, if the data is smaller or more orderly, the insertion sort algorithm is the best choice for three simple algorithms, which is better for a larger amount of data, and a quick sort algorithm later.


In addition to the speed comparison algorithm, there is a measure of the algorithm is how much memory space required by the algorithm. These three simple algorithms require virtually no additional memory space other than the initial array. All sorting algorithms are
An additional variable is required to store the data item at the time of exchange.


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.