Class template, linked list, direct insert sort, select sort, bubble sort

Source: Internet
Author: User

Class Template: Enables a user to declare a pattern for a class, make certain data members in a class, parameters of some member functions, return values of some member functions, and can take any type.
Class template, the class name must be followed by the < template parameter identifier list >
When you define an object in the main program, the class needs to describe what type of object it is, and also use <> parentheses to specify the type of the desired class template in the <> angle brackets.
Class Template: template< template parameter Table >
Class name
{Class member Declaration}
To define member functions outside of the class template, take the template< template parameter table, and specify which class the member function belongs to, and the template class name must be followed by the < template parameter identifier list >

Assignment and copy constructors: The copy constructor is exactly the same as an existing object when the object is just born.
An assignment is an object that has been there for some time, and then assigns the value of another object to it, which means that the two objects were once different and were once separate, but now let their state, their values, change the same.

Overloaded pointer conversion operators: object names cannot be automatically converted to corresponding pointer addresses, their types do not match, objects cannot be converted to pointer types, and by default they cannot be implemented and must be overloaded.
Convert the object name of the Arry class to a pointer of type T:
template<class T> Arry<T>::operator T *() {return list}

A linked list is a dynamic data structure that can be used to represent a linear group of sequential accesses. (linear groups are ordered by position, can distinguish between the first element, the second element)
Linked lists are made up of series nodes that can be dynamically generated at run time, with each node containing data fields and pointers to the next node of the list (that is, the address of the next node)
The only way to find each node is through the precursor node, not to turn the pointer, the table head pointer can not move, always point to the table header, the list has a cursor to point to the other nodes in the middle of the list, a single-linked list of working pointers two, one point to the current node, and the other is a pointer to the current node Two simultaneous moves to find the data in the linked list, delete the data, and traverse the data.

When inserting a node, the subsequent pointer to the current node must be assigned to the new node, and if the address of the new node is given directly to the current node, then the address of the next pointer is lost and the second half of the list cannot be found.

When you delete a node, need to determine whether the node to be deleted is the end of the node, if it is the end of the node, then directly delete, nothing to do, otherwise the current node of next assignment to the new pointer, the new pointer operation, the new pointer to the next assignment to the current node, replace the current node in the original next address.

Insert directly:
The outer loop controls the total number of comparisons, and the memory loop compares the current insertion value to the value previously inserted.
From a collection, the element is removed from the previous sequence, first compared to the end element, and if it is smaller, the end element is moved backward, compared to the previous element, until it is stopped more than the previous element, and the element is inserted.

template <class T>void insertionSort(T a[], int n) {int i, j;T temp;for (int i = 1; i < n; i++) {int j = i;T temp = a[i];while (j > 0 && temp < a[j - 1]) {a[j] = a[j - 1];j--;}a[j] = temp;}}

Select Sort:
There is an intermediary indexleast, first assign the address of the first element to it, with the value of the address, take this value in turn to compare, when the value is found smaller than it, change the address in the Indexleast, replaced by a small address number, It is then exchanged with the address of the numeric value that is compared, and after the exchange, it continues to be compared with the value in the Indexleast address.

template <class T>void mySwap(T &x, T &y) {T temp = x;x = y;y = temp;}template <class T>void selectionSort(T a[], int n) {for (int i = 0; i < n - 1; i++) {int leastIndex = i;for (int j = i + 1; j < n; j++)if (a[j] < a[leastIndex])leastIndex = j;mySwap(a[i], a[leastIndex]);}}

Bubble Sort:
To sort a sequence with n elements in ascending order of bubbles:
? First the first element is compared to the second element, and in reverse order, the two element is exchanged. and then compare
The second, third element, and so on, until the n-1 and nth elements are compared and exchanged. This process is called
For the first trip to bubble sort. After the first trip, the largest element is swapped to nth position.
? A second bubbling sort of the first n-1 element, with the largest element exchanged to the n-1 position.
? So continue until a certain trip is sorted without any interchange, sorting is complete. A sequence of n elements, starting with a
Bubble sequencing requires up to n-1 trips.

#include<iostream>using namespace std;template<class T>void myswap(T &x, T &y){    T temp = x;    x = y;    y = temp;}template<class T>void bubblesort(T a[], int n){    int i = n - 1;    while (i > 0)  //发生交换,还需要进行下一趟    {        int lastexchageindex = 0;//用来控制比较次数        for (int j = 0; j < i; j++)        {            if (a[j + 1] < a[j])                myswap(a[j + 1], a[j]);            lastexchageindex = j;  //j始终会比i少1,每次正好沉底一个数        }        i = lastexchageindex;//将比当前次数i少1的值再次赋值给i,用来控制下一次循环所需比较的次数    }}int main(){    int a[6] = { 8, 5, 9, 2, 5 };    bubblesort(a, 6);    for (auto &number : a)    {        cout << number << endl;    }    return 0;}

Class template, linked list, direct insert sort, select sort, bubble sort

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.