Row height: Bubble and insert sort. Do not care about others, their own design is their own.

Source: Internet
Author: User
Tags int size

A classification of algorithm problems: row height.

Background: There are a number of values, there are large and small, because of a certain purpose (such as 22 groups to minimize the maximum and the largest, such as accommodating the most and not more than a certain value of elements, etc.), you need to rank them first.

An array of values, how to drain the elements inside (e.g. from small to earth)?

650) this.width=650, "src=" http://ot2wtzcnv.bkt.clouddn.com/algorithm/sort.jpg "alt=" discharge high and low "title=" "style=" border: None;height:auto; "/>

22 comparison is not a problem, to solve these two problems:

* Who is better than who, how to arrange? * How to deal with the problem, how to make it smaller? It's all up to you to design! Design principle: Let the problem become smaller, eventually not.

Can not be arbitrarily compared, such as the arrangement of the singular elements to compare, this design can not see to solve the problem.

Can not be compared to the end but no further action, only compare but do not record and do not change the position, obviously no meaning.

Who comes to compare, and how to dispose of differently, different arrangements, to produce different algorithms.

To simplify your thinking, you can only consider situations where there are only two elements.

Like what:

* Let N1 compare with N2, if n1>n2, then value swap position, let N1 minimum. Then, continue to let N1 and N3 compare, the same smallest put to N1 ". When all is compared, the first position is the smallest, then the second position is considered again (the problem scale is converging). This is the "bubble sort" algorithm.

* Let N1 compare to the last, and then the second to the bottom, ..., the small one put to N1. Consider N2 .... This is the "bubble sort" algorithm.

* Let N1 and N2, N2 and N3 than ..., the big people put in the back. A round down, the final position is the maximum value, and then let go of this position and repeat. This is the "bubble sort" algorithm.

* Simulate the queue (from one to the beginning), from left to right, find an element higher than yourself, to the front. This is "insert sort".

* The first one compares to the last one, the second to the second to the bottom, the smaller one to the left, the big one to the right. This is the first form of "quick sort".

Notice, "Who is better than" do not have omissions, otherwise unfair.

Above, more want to explain, you can design yourself , not necessarily from the known algorithm to think. It also shows the bubble sort , the idea of inserting sort and the initial type of quick sort .


#include <stdio.h>

#include <stdlib.h>

#include <string.h>


void Bubble1 (int* arr, int size) {

for (int i = 0; i < size-1; i + +) {

for (int j = i+1; j < size; J + +) {

if (Arr[i] > Arr[j]) {

Arr[i]=arr[i] ^ arr[j];

Arr[j]=arr[i] ^ arr[j];

Arr[i]=arr[i] ^ arr[j];

}

}

}

}


void Bubble2 (int* arr, int size) {

for (int i = 0; i < size-1; i + +) {

for (int j = size-1; j > i; J--) {

if (Arr[i] > Arr[j]) {

Arr[i]=arr[i] ^ arr[j];

Arr[j]=arr[i] ^ arr[j];

Arr[i]=arr[i] ^ arr[j];

}

}

}

}


void Bubble3 (int* arr, int size) {

int count = size;

while (count) {

for (int i = 0; i < count-1; i + +) {

if (Arr[i] > arr[i+1]) {

Arr[i]=arr[i] ^ arr[i+1];

Arr[i+1]=arr[i] ^ arr[i+1];

Arr[i]=arr[i] ^ arr[i+1];

}

}

Count--;

}

}


Multiple use of a temporary group

void Insertsort (int* arr, int size) {

int* tmparr= (int*) malloc (sizeof (int) * size);

memcpy (Tmparr, arr, size*sizeof (int.));

int count = 0;

for (int i = 0; i < size; I + +) {

int j=0;

for (j = 0; J < Count; J + +) {

if (Arr[i]<tmparr[j]) {

memcpy (Tmparr+j+1, Tmparr+j, (size-j-1) *sizeof (int));

Tmparr[j]=arr[i];

Break

}

}

if (J==count) {

Tmparr[j]=arr[i];

}

Count + +;

}

memcpy (arr, Tmparr, size*sizeof (int.));

Free (Tmparr);

}


In-place insert

void Insertsort2 (int* arr, int size) {

for (int i = 0; i < size; I + +) {

for (int j = 0; J < i; J + +) {

if (Arr[i] < arr[j]) {

int t = arr[i];

memcpy (Arr+j+1, Arr+j, (i-j) *sizeof (int));

arr[j]=t;

Break

}

}

}

}


int main (int argc, char *argv[])

{

int arr[] = {5, 3, 6, 1, 2};

int size = sizeof arr/sizeof *arr;

/*bubble3 (arr, size); */

Insertsort2 (arr, size);

for (int i = 0; i < size; I + +) {

printf ("%d,", arr[i]);

}

return 0;

}


This article from the "13126504" blog, reproduced please contact the author!

Row height: Bubble and insert sort. Do not care about others, their own design is their own.

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.