欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Data-Structure-Notes

程序员文章站 2022-12-21 23:12:56
Data Structure Notes Chapter 1 Sorting Algorithm Selection Sorting: Bubble Sorting: Shell Sorting: Merge Sorting: Tips1 :Merge Sort Optimize in nearly ......

data structure notes

chapter-1 sorting algorithm

  • selection sorting:
/*
*   selection sort
*/
template<typename t>
void selectionsort(t arr[], int n) {
    for (int i = 0;i < n;i++) {
        int minindex = i;
        for (int j = i + 1;j < n;j++) {
            if (arr[j] < arr[minindex])
                minindex = j;
        }
        swap(arr[i], arr[minindex]);
    }
}

// from both ends to exchange the elements in original array, it's a better solution optimize the previous selection sort.
template<typename t>
void optimizedselectionsort(t arr[], int n) {

    int left = 0, right = n - 1;
    while (left < right) {
        int minindex = left;
        int maxindex = right;

        // in each rounds must assure arr[minindex] <= arr[maxindex]
        if (arr[minindex] > arr[maxindex])
            swap(arr[minindex], arr[maxindex]);

        //traversing the array to choose the match positon.
        for (int i = left + 1; i < right; i++)
            if (arr[i] < arr[minindex])
                minindex = i;
            else if (arr[i] > arr[maxindex])
                maxindex = i;

        swap(arr[left], arr[minindex]);
        swap(arr[right], arr[maxindex]);

        left++;
        right--;
    }

    return;
}
  • bubble sorting:
/*
*   bubblesort
*/
template<typename t>
void bubblesort(t arr[], int n) {

    bool swapped;

    do {
        swapped = false;
        for (int i = 1; i < n; i++)
            if (arr[i - 1] > arr[i]) {
                swap(arr[i - 1], arr[i]);
                swapped = true;

            }

        // 优化, 每一趟bubble sort都将最大的元素放在了最后的位置
        // 所以下一次排序, 最后的元素可以不再考虑
        n--;

    } while (swapped);
}


// 我们的第二版bubblesort,使用newn进行优化
template<typename t>
void optimizedbubblesort(t arr[], int n) {

    int newn; // 使用newn进行优化

    do {
        newn = 0;
        for (int i = 1; i < n; i++)
            if (arr[i - 1] > arr[i]) {
                swap(arr[i - 1], arr[i]);

                // 记录最后一次的交换位置,在此之后的元素在下一轮扫描中均不考虑
                newn = i;
            }
        n = newn;
    } while (newn > 0);
}
  • shell sorting:
template<typename t>
void shellsort(t arr[], int n) {

    // 计算 increment sequence: 1, 4, 13, 40, 121, 364, 1093...
    int h = 1;
    while (h < n / 3)
        h = 3 * h + 1;

    while (h >= 1) {

        // h-sort the array
        for (int i = h; i < n; i++) {

            // 对 arr[i], arr[i-h], arr[i-2*h], arr[i-3*h]... 使用插入排序
            t e = arr[i];
            int j;
            for (j = i; j >= h && e < arr[j - h]; j -= h)
                arr[j] = arr[j - h];
            arr[j] = e;
        }

        h /= 3;
    }
}
  • insert sorting: 对于近乎有序的数组可以降到$ o(n)$的时间复杂度。
template<typename t>
void binaryinsertionsort(t arr[], int n) {
    int i, j, low, high, mid;
    for (i = 1;i < n;i++) {
        t e = arr[i];
        
        //binary searching in the ordered range of array.
        low = 0; high = i - 1;
        while (low<= high)
        {
            mid = (low + high) / 2;
            if (arr[mid] > e) high = mid - 1;
            else low = mid + 1;
        }
        //moving elements.
        for (j = i - 1;j >= high + 1;--j) {
            arr[j + 1] = arr[j];
        }
        arr[high + 1] = e;
    }
}

template<typename t>
void optimizedinsertionsort(t arr[], int n) {
    for (int i = 1;i < n;i++) {

        // find right position without exchange frequently.
        t e = arr[i];
        int j;
        for (j = i;j > 0 && arr[j - 1] > e;j--) {
            arr[j] = arr[j - 1];
        }
        arr[j] = e;
    }
}
  • merge sorting:
    • tips1:merge sort optimize in nearly ordered array
    void __mergesort(t arr[], int l, int r) {
        if (l >= r) return;
    
        int mid = (l + r) / 2;      // variable 'mid' may overflow
        __mergesort(arr, l, mid);
        __mergesort(arr, mid+1, r);
        if(arr[mid] > arr[mid+1])   // optimize in nearly ordered array.
            __merge(arr, l, mid, r);
    }
    • tips2:when the sorting range of array in a short length, using insertsort replace mergesort can be more faster.
     template<typename t>
    void __mergesort(t arr[], int l, int r) {
        //if (l >= r) return;
        if (r - l <= 15) {           // the '15' is a constant represent the minmum judge range.
            insertionsort(arr, l, r);
            return;
        }
        int mid = (l + r) / 2;      // variable 'mid' may overflow
        __mergesort(arr, l, mid);
        __mergesort(arr, mid+1, r);
        if(arr[mid] > arr[mid+1])   // optimize in nearly ordered array.
            __merge(arr, l, mid, r);
    }
  • botton to up merge sorting : the algorithm can be usd in the linkedlist . the original mergesort may preform better than this algorithm in normal situation.
    • standard
    template<typename t>
    void mergesortbottontoup(t arr[], int n) {
        for(int size = 1; size <= n; size += size)
            // in order to assure exist two sperate array, setting (i+size < n) not (i < n)
            for (int i = 0; i + size < n ; i += size + size) {      
                // merge arr[i ... i+size-1] and arr[i+size ... i+2*size-1]
                // in order to assure latter array isn't overflow so use min(i + size + size - 1, n-1) to choosing a right part.
                __merge(arr, i, i + size - 1, min(i + size + size - 1, n-1));
            }
    }
    • optimization
    template <typename t>
    void mergesortbu2(t arr[], int n){
    
        // 对于小规模数组, 使用插入排序
        for( int i = 0 ; i < n ; i += 16 )
            insertionsort(arr,i,min(i+15,n-1));
    
        // 一次性申请aux空间, 并将这个辅助空间以参数形式传递给完成归并排序的各个子函数
        t* aux = new t[n];
        for( int sz = 16; sz <= n ; sz += sz )
            for( int i = 0 ; i < n - sz ; i += sz+sz )
                // 对于arr[mid] <= arr[mid+1]的情况,不进行merge
                // 对于近乎有序的数组非常有效,但是对于一般情况,有一定的性能损失
                if( arr[i+sz-1] > arr[i+sz] )
                    __merge2(arr, aux, i, i+sz-1, min(i+sz+sz-1,n-1) );
        delete[] aux; // 使用c++, new出来的空间不要忘记释放掉:)
    }
    
  • quicksort (divide-and-conquer algorithm)
    • partition

    • insert sort optimization
        // sort the range of [l ... r]
    template <typename t>
    void __quicksort(t arr[], int l, int r) {
        //if (l >= r) return;
        if (r - l <= 15) {
            optimizedinsertionsort(arr, l, r);
            return;
        }
        int p = __partition(arr, l, r);
        __quicksort(arr, l, p - 1);
        __quicksort(arr, p + 1, r);
    }
    • optimization in the face of nearly ordered array
      compare to mergesort, the sorting tree generate by quick sort is more unbalanced.the worst situation the effience of quick sort can be deteriorate to $o(n^2)$
      tradinational method using the left element to be demarcating element. in order to solving the problem, we select the demarcating element randomly.

      ```cpp

    template
    int __partition(t arr[], int l, int r) {

      swap(arr[l], arr[rand() % (r - l + 1) + l]);  // add this process to randomly choose demarcating element.
      t v = arr[l];
    
      //arr[l+i ... j] < v;arr[j+1 ... i] > v
      int j = l;
      for (int i = l + 1;i <= r;i++) {
          if (arr[i] < v) {
              swap(arr[j + 1], arr[i]);
              j++;
          }
      }
    
      swap(arr[l], arr[j]);
      return j;

    }

    template
    void quicksort(t arr[], int n) {
    srand(time(null)); // the partial of randomly select.
    __quicksort(arr, 0, n - 1);
    }

    ```

    • optimization in the face of many repeating numbers. (dual qucik sort)
      when face many repeating numbers, the speration of array may unbalanced. in this situation, quick sort can be degraded to $o(n^2)$.

    solution :

    template <typename t>
    int __partition2(t arr[], int l, int r) {
        swap(arr[l], arr[rand() % (r - l + 1) + l]);  // add this process to randomly choose demarcating element.
        t v = arr[l];
    
        //arr[l+i ... j] < v; arr[j+1 ... i] > v
        int i = l + 1, j = r;
        while (true) {
            //from front to behind to find a even bigger number.
            //from behind to front to find a even smaller number.
            while (i <= r&& arr[i] < v) i++;
            while (j >= l + 1 && arr[j] > v) j--;
            if (i > j) break;
            swap(arr[i], arr[j]);
            i++;
            j--;
        }
    
        swap(arr[l], arr[j]);
    
        return j;
    }
    
    • optimization in the face of many repeating numbers. (qucik sort 3 ways)
    template <typename t>
    void __quicksort3(t arr[], int l, int r) {
        //if (l >= r) return;
        if (r - l <= 15) {
            optimizedinsertionsort(arr, l, r);
            return;
        }
    
        // partition
        swap(arr[l], arr[rand() % (r - l + 1) + l]);
        t v = arr[l];
    
        int lt = l;     //arr[l+1 ... lt] < v
        int gt = r + 1; //arr[gt ... r] > v
        int i = l + 1;  //arr[lt+1 ... i] == v
        while (i < gt) {
            if (arr[i] < v) {
                swap(arr[i], arr[lt + 1]);
                lt++;
                i++;
            }
            else if(arr[i] > v) {
                swap(arr[i], arr[gt - 1]);
                gt--;
            }
            else {// arr[i] == v
                i++;
            }
        }
    
        swap(arr[l], arr[lt]);
    
        __quicksort3(arr, l, lt - 1);
        __quicksort3(arr, gt, r);
    }
    
    template <typename t>
    void quicksort(t arr[], int n) {
        srand(time(null));      // the partial of randomly select.
        __quicksort3(arr, 0, n - 1);
    }