Simple sorting and sorting algorithms

Source: Internet
Author: User

Simple sorting and sorting algorithms

The job is in the countdown state. We plan to review the data structure and related algorithms. We expect to spend two days sorting the algorithms we have seen. First, let's take a look at the time complexity of O (n2).

First, refer to the big data structure to define a linked list class:

<pre name="code" class="cpp">#include <iostream>#define MAXSIZE 1000using namespace std;class SqList{public:SqList():length(0){}SqList(int length1,int value=0):length(length1){for(int i=0;i<length;++i){data[i]=value;}}bool insertTail(int value){if(length>=MAXSIZE){return false;}data[length]=value;length++;}friend ostream& operator<<(ostream& output, SqList list);public:int data[MAXSIZE];int length;};void swap(int& a,int &b){int tmp=a;a=b;b=tmp;}ostream& operator<<(ostream& output, SqList list){for (int i = 0; i<list.length; ++i){output <<list.data[i] << "   ";if ((i + 1) % 18 == 0)output << endl;}output << endl;return output;}


Bubble sorting method:

/*** The Bubble Sorting means that the adjacent two are compared with each other. Remember to move the larger or smaller forward or backward as needed, the comparison between two adjacent values is one of the characteristics of Bubble Sorting */void BubbleSort1 (SqList * list) {// shift the larger value to int length = list-> length each time; while (length> 0) {for (int I = 0; I <length-1; ++ I) {if (list-> data [I]> list-> data [I + 1]) swap (list-> data [I], list-> data [I + 1]);} length -- ;}} void BubbleSort2 (SqList * list) {// each time, move a small forward for (int I = 0; I <list-> length; I ++) {for (int j = list-> length-2; j> = I; j --) {if (list-> data [j]> list-> data [j + 1]) swap (list-> data [j], list-> data [j + 1]) ;}}

Select sorting method:

/*** Select sorting: select a minimum value in the unordered queue each time, and then exchange it with the I value until I is length. * Of Course, you can also select the maximum value to be followed, depending on your needs */void selectSort (SqList * list) {for (int I = 0; I <list-> length; ++ I) {int min = list-> data [I]; int pos = I; for (int j = I + 1; j <list-> length; ++ j) {if (list-> data [j] <min) {min = list-> data [j]; pos = j ;}} if (pos! = I) {swap (list-> data [I], list-> data [pos]) ;}}

Simple insert sorting method:

<Span style = "font-size: 14px;">/*** in a traversal table, insert each element to the correct position */void InsertSort1 (SqList * list) {for (int I = 1; I <list-> length; ++ I) {int j = I-1; for (; j> = 0; j --) {if (list-> data [I]> list-> data [j]) break;} int tmp = list-> data [I]; for (int k = I; k> j + 1; -- k) {list-> data [k] = list-> data [k-1];} list-> data [j + 1] = tmp ;}} void InsertSort2 (SqList * list) {for (int I = 1; I <list-> length; ++ I) {if (list-> data [I] <list-> data [I-1]) {int tmp = list-> data [I]; int j = I-1; for (; j> = 0 & list-> data [j]> tmp; -- j) {// when searching, perform the back-shift operation list-> data [j + 1] = list-> data [j];} list-> data [j + 1] = tmp ;}}} </span>

Hill sorting method (improvement of simple insertion sorting ):

/*** Hill sorting is an improvement of insert sorting. It can be understood that an array is divided into several small arrays for insertion sorting, and then merged to make the original array basically orderly. * A key step for hill sorting is incremental selection. An appropriate increment can improve the sorting efficiency, but an inappropriate increment may cause program crashes or incorrect results. * Second, Hill sorting is not a stable Sorting Algorithm because it jumps into sorting. * Hill sorting is only slightly better than the previous several types of O (n2), and is not better than the fast sorting algorithms mentioned later. */Void ShellSort (SqList * list) {int increment = list-> length; do {increment = increment/3 + 1; for (int I = increment + 1; I <list-> length; ++ I) {if (list-> data [I] <list-> data [I-increment]) {int tmp = list-> data [I]; int j = I-increment; for (; j> = 0 & list-> data [j]> tmp; j-= increment) {list-> data [j + increment] = list-> data [j];} list-> data [j + increment] = tmp ;}}} while (increment> 1 );}








Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.