simple implementation of swap
C language Mode (by-pointer): Template <typename type>bool swapbypointer (Type *pointer1, type *pointer2) { // Make sure that 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 + + specific mode (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 the swap ourselves, we would prefer to use C + + STL has implemented the STD::SWAP () function, which exists in the namespace STD, using examples such as the following < bubble sort;
Bubble Sort (bubble-sort)
Algorithm idea:
Scan data from left to right, find the largest element, and put it to the right of the array;
Process:
The loop compares the adjacent two numbers, and if the number on the left is larger than the right one, then two numbers are exchanged;
//implemented: Note the three note points 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): Ensure that once the array is ordered, it will stop the sort directly and will not continue with the useless cyclic bool Isorder = false; Outer loop control number of scans (LENGTH-1)//Note point (2): n elements in fact just N-1 scan for (int i = 0;!isorder && i < length-1; ++i) { First assume that this array is already ordered Isorder = true; Note (3): Be sure to be able to scan from 0 to the last unsorted element for (Type *iter = begin; iter < end-i-1; ++iter) {//If the front left element &G t; the right element 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 (select-sort)
Thought:
Selects a minimum element from a sequence that is not currently sorted, placing it at the end of the queue of a sorted sequence;
Points:
1. Note the meaning represented by three pointers (inner, outer, miner);
2. Also note that the smallest element is searched from an unordered sequence !
Implement template <typename type>void selectsort (Type *begin, type *end) { if (begin = = end) | | (begin = NULL) | | (end = NULL)) return; Just loop to the previous one of the last element, because the remaining one is definitely the largest for (Type *outer = begin; outer < end-1; ++outer) { //NOTE: is to find from a sequence that has not been sorted (miner = outer, inner = outer+1) Type *miner = outer; Iterate through the array from miner+1, looking for an element value less than *miner for (Type *inner = outer+1; inner < end; ++inner) { if (*inner < *mi NER) miner = inner; } if (Miner! = outer) Std::swap (*miner, *outer);} } In order to enable the STL standard container such as vector using 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 Bubble-sort and select-sort ourselves, we are generally not used in actual software development, because of its efficiency is O (n^2), efficiency is too slow ^_^, so we still recommend the use of C + + STL has implemented Std::sort () , its internal principle uses the quick sort, the efficiency is O (LOGN) speed very fast.
Attached-Test procedure
int main () { Srand (Time (NULL)); Vector<double> Dvec; int count = ten; 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 &L t;< Endl; } return 0;}
Data Structure Basics (1)--swap & Bubble-sort & Select-sort