close

This chapter introduces the basic concept of selection sorting and bubble sorting. In the programming content of sorting, the most famous is undoubtedly selective sorting and bubble sorting. Today, let us look at the specific code implementation of these two sorting schemes.

1. Selection Sorting

The idea of sorting by selection method:

Compare the first place with all the others, as long as it is smaller than the first place, move to the first place.

After the comparison, the first place is the smallest.

Then compare the second place with all the rest, as long as it is smaller than the second place, then switch to the second place.

After the comparison, the second place is the second smallest and so on.

The specific code is as follows:

public class sort {

            public static void main(String[] args) {

                        int a [] = new int[]{10,30,20,50,40};

                        //Before sorting, print out the content

                        for (int i = 0; i < a.length; i++) {

                                    System.out.print(a[i] + ” “);

                        }

                        System.out.println(” “);

                        //Selection order

                        //Step 1: Compare the first bit with all other bits

                        //If it is found that the data in other locations is smaller than the first one, exchange it

                        for (int i = 1; i < a.length; i++) {

                                    if(a[i]<a[0]){  

                                                int temp = a[0];

                                                a[0] = a[i];

                                                a[i] = temp;

                                    }

                        }

                        //Print out the content

                        //It can be found that the smallest number is at the top

                        for (int i = 0; i < a.length; i++) {

                                    System.out.print(a[i] + ” “);

                        }

                        System.out.println(” “);

                        //Step 2: Compare the second bit with all the remaining bits

                        for (int i = 2; i < a.length; i++) {

                                    if(a[i]<a[1]){  

                                                int temp = a[1];

                                                a[1] = a[i];

                                                a[i] = temp;

                                    }

                        }

                        //Print out the content

                        //It can be found that the penultimate number is in the second position

                        for (int i = 0; i < a.length; i++) {

                                    System.out.print(a[i] + ” “);

                        }

                        System.out.println(” “);              

                        //Can find a pattern

                        //The moving position is gradually increased from 0

                        //So you can put a layer of loop on the outside

                        for (int j = 0; j < a.length-1; j++) {

                                    for (int i = j+1; i < a.length; i++) {

                                                if(a[i]<a[j]){  

                                                            int temp = a[j];

                                                            a[j] = a[i];

                                                            a[i] = temp;

                                                }

                                    }

                        }

                        //Prent out the content

                        for (int i = 0; i < a.length; i++) {

                                    System.out.print(a[i] + ” “);

                        }

                        System.out.println(” “);              

            }

}

The final output of the console:

2. Bubble Sorting

The idea of bubbling sorting:

The first step: starting from the first digit, compare the two adjacent ones.

If it is found that the former is larger than the latter, the larger data is exchanged at the back. After the cycle comparison is completed, the last bit is the largest.

Step 2: Do it again, but don’t compare the last digit and so on.

The specific code is as follows:

public class sort {

    public static void main(String[] args) {

        int a [] = new int[]{10,30,20,50,20};

        //Before sorting, print out the content

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);

        //Bubble sort

        //The first step: starting from the first digit, compare the two adjacent ones

        //If it is found that the front is larger than the back, then the big data is exchanged at the back

        for (int i = 0; i < a.length-1; i++) {

            if(a[i]>a[i+1]){  

                int temp = a[i];

                a[i] = a[i+1];

                a[i+1] = temp;

            }

        }

        //Print out the content

        //It can be found that the biggest is at the end

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);

        //Step 2: Do it again, but don’t compare the last digit

        for (int i = 0; i < a.length-2; i++) {

            if(a[i]>a[i+1]){  

                int temp = a[i];

                a[i] = a[i+1];

                a[i+1] = temp;

            }

        }

        //Print out the content

        //It can be found that the penultimate is in the penultimate position

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);       

        //You can find a pattern

        //The back boundary is shrinking

        //So you can put a layer of loop on the outside

        for (int j = 0; j < a.length; j++) {

            for (int i = 0; i < a.length-j-1; i++) {

                if(a[i]>a[i+1]){  

                    int temp = a[i];

                    a[i] = a[i+1];

                    a[i+1] = temp;

                }

            }

        }

        //Print out the content

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);       

    }

}

The final output of the console:

3. Exercise-Sort

First create an array of length 8 and fill it with random numbers.

First use the selection method to sort.

Then use the bubble method to sort it backwards.

The specific code is as follows:

public class sort {

    public static void main(String[] args) {

        int a[] = new int[8];

        // Fill with random numbers

        for (int i = 0; i < a.length; i++)

            a[i] = (int) (Math.random() * 100);

        // Before the sequence, print out the content

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);

        // Selection order

        for (int j = 0; j < a.length – 1; j++) {

            for (int i = j + 1; i < a.length; i++) {

                if (a[i] < a[j]) {

                    int temp = a[j];

                    a[j] = a[i];

                    a[i] = temp;

                }

            }

        }

        // Print out the content

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println();

        // Bubble sort

        for (int j = 0; j < a.length; j++) {

            for (int i = 0; i < a.length – j – 1; i++) {

                if (a[i] < a[i + 1]) {

                    int temp = a[i];

                    a[i] = a[i + 1];

                    a[i + 1] = temp;

                }

            }

        }

        // Print out the content

        for (int i = 0; i < a.length; i++) {

            System.out.print(a[i] + ” “);

        }

        System.out.println(” “);

    }

}

The final output of the console:

By studying the concepts of selection sorting and bubble sorting, you can define your own array sorted from small to large. These two sorting methods will give you good ideas for designing sorting code.

Tags : bubble sortingselection sortingsmallest.

1 Comment

Leave a Response