Data Structure and Algorithms - Sorting and Searching (java Description)

Keywords: less

In software development, there are two common tasks, one is to find a specific element in a group, the other is to sort a group of elements in a specific order. We can use a variety of algorithms to accomplish these tasks, and the differences between these algorithms are worthy of our careful study. Next, let's explore these algorithms.

I. Search

1.1. Linear Search Method
Linear lookup is to traverse every element of array data by index. If we find that the target element to be searched is the same as one element in array data, we will return the found element. Of course, we can also improve it by directly returning the found element.

public static <T extends Comparable<? super T>> 
boolean linearSearch(T[] data, int min , it max, T target)
{
    int index = min;
    boolean found = false;
    while(!found && index <= max){//Judging boundaries
        if(data[index].compareTo(target) == 0){
            found = true;
        }
        index++;
    }
    return found;
}

1.2. Binary Search Method
Binary search method is more efficient than linear search method, because it saves a lot of time without traversing all the time. The method we use here is recursive call to search. If we find that the size of the element in the midpoint position is smaller than the target element, we will index the midpoint position + 1 and make recursive call if it is larger than the target element. The labeled element indexed the midpoint position to - 1 and made a recursive call. The recursive exit is to return the result when the value of the midpoint position is equal to the value of the target element.

public static <T extends Comparable<? super T>> 
boolean binarySearch(T[] data, int min , it max, T target)
{
    boolean found = false;
    int midpoint = (min+max)/2;//Choose the midpoint position
    if(data[midpoint].compareTo(target) == 0){
        found = true;
    } else if(data[midpoint].compareTo(target) > 0){
        if(min <= midpoint - 1){
            found = binarySearch(data, min, midpoint - 1, target);
        }
    } else if(data[midpoint].compareTo(target) > 0){
        if(max >= midpoint + 1){
            found = binarySearch(data, min, midpoint + 1, target);
    }
}

Sorting

The order is divided into
Sequential Sorting: Selection, Insertion, Bubble Sorting;
Logarithmic Sorting: Fast, Merge Sorting;

2.1. Selective Sorting
By scanning the entire list to find the minimum value, the value is exchanged with the value of the first place in the list. Scan the remaining (except the first value) part of the list and find the minimum value, then exchange it with the value at the second position of the list. And so on.

public static <T extends Comparable<? super T>> 
void selectionSort(T[] data){
    int min ;//Define the minimum value of storage scan
    T temp;
    for(itn index = 0; index < data.length - 1; index++){
        min = index;//Initialized to the minimum value of the first
        for(int scan = index + 1; scan < data.length - 1; scan++){
            if(data[scan].compareTo(data[min]) < 0){//If it is less than the minimum, assign the minimum found to the current minimum
                min = scan;
            }
        }
        //Exchange the minimum value found and the current first location
        temp = data[min];
        data[min] = data[index];
        data[index] = temp;
    }
}

The outer loop controls where the next minimum value will be stored in the array, and the inner loop searches for the minimum value of the list of the remaining parts by scanning all locations larger than or equal to the indexing of the outer loop.

Here we extract an interchangeable function:

//Exchange position of two elements
private static <T extends Comparable<T>> void swap
(T[] data, int index1, int index2){
    T temp = data[index];
    data[index1] = data[index2];
    data[index2] = temp;
}

2.2. Insertion sort
The insertion sorting algorithm completes the sorting of the list by repeatedly inserting a specific value into a sorted subset of the list.
Strategy: Sort the first two values in the list according to their relative size, insert the third value of the list into the appropriate position of the first two sorted values, and then insert the fourth value into the appropriate position of the first three sorted values.

public static <T extends Comparable<? super T>> 
void selectionSort(T[] data){
    for(int index = 1; index < data.length; index++){
        T key = data[index];//Save this element temporarily
        int position = index;//Outer loop saves index
        //Find larger elements to the right location
        while(position > 0 && data[position - 1].compareTo(key) > 0){
            data[position] = data[position - 1];
            position --;
        }
        data[position] = key;
    }
}

2.3. Bubble Sorting
Repeated comparison lists of adjacent elements, if found that the location of an adjacent element is wrong, the position of the two elements will be exchanged.

public static <T extends Comparable<? super T>> 
void selectionSort(T[] data){
    int position ,scan;
    T temp;
    for(position = data.length - 1; position >=0; position --){
        for(scan = 0; scan <= position - 1; scan++){
            if(data[scan].compareTo(data[scan+1]) > 0){
                swap(data,scan, scan + 1);
            }
        }
    }
}

2.4. Quick Sorting
The list is partitioned by arbitrarily selected partition elements, and the sublists on either side of the partition elements are sorted recursively.

Posted by cap2cap10 on Mon, 15 Jul 2019 15:57:00 -0700