[Data Structure] Sorting Algorithms - Selection Sort and Heap Sort

selection sort

1. Basic idea

  Taking ascending order as an example, assuming there are n pieces of data, each pass selects the data element with the smallest key code in the set of data elements to be sorted in the following ni, as the i-th element of the ordered sequence, until the set to be sorted has only 1 element left.

2. Operation steps

  As an example:
write picture description here

3. Algorithm performance

  Time complexity: The direct selection algorithm needs to traverse each time to select the smallest number, and traverse n times, the time complexity is O(N^2)
  Stability: It is an unstable algorithm.

void SelectSort(int* array, int size)
{
    for (size_t i = 0; i < size-1; i++)
    {
        size_t maxPos = 0;
        for(size_t j = 1; j < size - i; j++)
        {
            if(array[j] > array[maxPos])
                maxPos = j;
        }

        if(maxPos != size - i - 1)
            swap(array[maxPos], array[size - i - 1]);
    }
}

We can improve this direct selection sorting method. In the above algorithm, we find the smallest data from the front to back ni (i starts from 0) data each time, and put it in the i-th position; Change it here, not only to find the lowest data from front to back, but also to find the largest data from back to front, and put them in the corresponding positions, so that we can run a few times less.

void SelectSortOP(int* array, int size)
{
    size_t begin = 0;
    size_t end = size-1;

    while(begin < end)
    {
        size_t maxPos = begin;
        size_t minPos = begin;
        size_t i = begin+1;
        while(i <= end)
        {
            if(array[i] > array[maxPos])
                maxPos = i;

            if(array[i] < array[minPos])
                minPos = i;
            i++;
        }

        if(maxPos != end)
            swap(array[maxPos], array[end]);

        if (minPos == end)//最小元素出现在最大元素的位置
            minPos = maxPos;

        if(minPos != begin)
            swap(array[minPos], array[begin]);

        begin++;
        end--;
    }
}

heap sort

1. Basic idea

Heap sorting refers to the sorting performed by the data structure of the heap, and the elements of the specified index are quickly located by using the characteristics of the array. Using the heap for sorting, the number of comparisons and the number of exchanges are less than that of bubble sort or selection sort, and the performance is higher.

2. Specific steps
  1. Create a heap: If you want to sort ascending, you need to create a large heap; descending, you need a small heap;
  2. Swap the element at the top of the heap with the current last element;
  3. The maximum number of heap elements minus 1;
  4. Adjust down to meet the definition of max heap
  5. Repeat steps 2-4 above until the array is empty.
3. Algorithm performance
  1. Time complexity: The time of heap sorting is mainly composed of the time overhead of creating a maximum heap and adjusting after each exchange of the top elements of the heap. The average time complexity of heap sorting is O(N*logN)
  2. Space complexity: in-place sorting, auxiliary space is O(1)
  3. Stability: is an unstable sorting method.
void Adjust(int* array, int size, size_t parent)
{
    size_t child = parent*2+1;

    while(child < size)
    {
        if(child+1 < size && array[child+1] > array[child])
            child += 1;

        if(array[parent] < array[child])
        {
            swap(array[parent], array[child]);
            parent = child;
            child = parent*2+1;
        }
        else
            break;

    }
}

void HeapSort(int* array, size_t size)
{
    // 1. 创建堆
    int root = (size-2)>>1;
    for(; root >= 0; --root)
        Adjust(array, size, root);

    // 2. 堆排序
    size_t end = size-1;
    while(end)
    {
        swap(array[0], array[end]);
        Adjust(array, end, 0);
        end--;
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325627098&siteId=291194637