Data Structure BASICS (1): Data Structure Basics

Source: Internet
Author: User

Data Structure BASICS (1): Data Structure Basics

Simple implementation of Swap
// C language (by-pointer): template <typename Type> bool swapByPointer (Type * pointer1, Type * pointer2) {// ensure that the two pointers do not point to the same object if (pointer1 = NULL | pointer2 = NULL) {return false;} if (pointer1! = Pointer2) {Type tmp = * pointer1; * pointer1 = * pointer2; * pointer2 = tmp;} return true ;}

// C ++ special method (by-reference): template <typename Type> void swapByReference (Type & value1, Type & value2) {if (value2! = Value1) {Type tmp = value1; value1 = value2; value2 = tmp ;}}

Summary:

Although we have implemented swap by ourselves, we recommend that you use the std: swap () function that has been implemented by C ++ STL. It is stored in the namespace std, use the following <bubble sort>.

Bubble-Sort)

Algorithm idea:

Scan data from left to right, find the largest element, and place it to the right of the array;

Process:

The loop compares two adjacent numbers. If the number on the left is greater than the number on the right, two numbers are exchanged;

// Implementation: Pay attention to the three points of attention in the Code (x): template <typename Type> void bubbleSort (Type * begin, Type * end) {if (begin = end) | (begin = NULL) | (end = NULL) return; int length = end-begin; // note (1): Once the array is ordered, the sorting will be stopped directly, and the useless loop bool isOrder = false will not be continued; // The number of scans in the outer loop (length-1) // note (2 ): N elements in fact only need to N-1 scanning for (int I = 0 ;! IsOrder & I <length-1; ++ I) {// first, it is assumed that this array has an ordered isOrder = true; // note (3 ): ensure that the last unordered element can be scanned from 0 for (Type * iter = begin; iter <end-i-1; + iter) {// if the element above the left> the element on the right is if (* iter> * (iter + 1) {// exchange std: swap (* iter, * (iter + 1); isOrder = false ;}}} template <typename Type> void bubbleSort (Type * array, int length) {return bubbleSort (array, array + length );}
Select-Sort)

Thoughts:

Select the smallest element from the unsorted sequence and put it at the end of the queue of the sorted sequence;

Key points:

1. Pay attention to the meanings of the three pointers (inner, outer, miner;

2. At the same time, it is important to find the smallest element in a sequence that has never been sorted!

// Implement template <typename Type> void selectSort (Type * begin, Type * end) {if (begin = end) | (begin = NULL) | (end = NULL) return; // you only need to loop to the first of the last element, because the remaining one must be the largest for (Type * outer = begin; outer <end-1; ++ outer) {// note: is to find (miner = outer, inner = outer + 1) Type * miner = outer; // traverse the array from miner + 1, find a for (Type * inner = outer + 1; inner <end; ++ inner) {if (* inner <* miner) m Whose element value is less than * miner Iner = inner;} if (miner! = Outer) std: swap (* miner, * outer) ;}// to enable STL standard containers such as vector to use template <typename Iterator> void selectSort (Iterator iter1, iterator iter2) {return selectSort (& (* iter1), & (* iter2);} template <typename Type> void selectSort (Type * array, int length) {return selectSort (array, array + length );}

Summary:

Although we have implemented the Bubble-Sort and Select-Sort by ourselves, we generally do not use them in actual software development because the efficiency is O (N ^ 2 ), the efficiency is too slow ^ _ ^, so we recommend using the std: sort () that has been implemented in C ++ STL. Its internal principle uses quick sorting, the efficiency is O (logN) speed is very fast.

 

Appendix-Test procedure

int main(){    srand(time(NULL));    vector<double> dVec;    int count = 10;    while (count --)    {        dVec.push_back((rand()%1000)/100.0);    }    selectSort(dVec.begin(), dVec.end());    for (vector<double>::iterator iter = dVec.begin(); iter < dVec.end(); ++iter)    {        cout << *iter << endl;    }    return 0;}

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.