Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to sort and find on C++

Shulou Source: shulou.com Published: 2022-06-01 01:40:46 09月25日 Update

This article mainly introduces how to sort and find C++, the article is very detailed, has a certain reference value, interested friends must read it!

1. Sort the array by bubbling (ascending order)

# include # include

Void sort (int a [], int n) {int I, j, t; for (I = 0; I

< n-1 ; i++) { for (j = 0; j < n-1; j++) { if (a[j] >

A [juni1]) {t = a [j]; a [j] = a [juni1]; a [juni1] = t;}

Int main () {int a [5], I; for (I = 0; I

< 5; i++) { printf("input a[%d]=", i); scanf("%d", &a[i]); } sort(a, 5); for (i = 0; i < 5; i++) { printf("%4d", a[i]); } system("pause"); return 0;} 2、用插入法排序将输入到数组中的元素进行排序 #include #include void fun(int a[], int n){ int k, j, t; for (j = 1; j < n; j++) { t = a[j]; k = j - 1; while (k >

= 0 & t > a [k]) {a [k + 1] = a [k]; kmuri;} a [k + 1] = t;}}

Int main () {int a [5], I; for (I = 0; I

< 5; i++) { printf("input a[%d]=", i); scanf("%d", &a[i]); } fun(a, 5); for (i = 0; i < 5; i++) { printf("%4d", a[i]); } system("pause"); return 0;} 3、用选择法将输入的10个数进行排序 #include #include int main(){ int i, j, min, tem, a[10]; for (i = 0; i < 10; i++) { printf("input a[%d]=", i); scanf("%d", &a[i]); } for (i = 0; i < 10; i++) { printf("%d", a[i]); } for (i = 0; i < 10 - 1; i++) { min = i; for (j = i + 1; j < 10; j++) { if (a[min] >

A [j]) {min = j;} tem = a [I]; a [I] = a [min]; a [min] = tem;} printf ("\ n"); for (I = 0; I < 10; iTunes +) {printf ("% d", a [I]);} system ("pause"); return 0;}

4. Using the half-and-half search method, you will check whether the entered number is in the ascending array of 10 elements, and if so, which number it is.

# include # include

Int main () {int a [10] = {12 int a [10] = {12 Peregra [10] = {12 Peregrine 32 [10] = {12 Peregrine 76pyrorn 85pyrorn]; int num, bott, top, mid; printf ("input a number:"); scanf ("% d", & num); bott = 0; top = 9; while (bott top) {printf ("no\ n");} system ("pause"); return 0;} above are all the contents of this article entitled "how to sort and find C++". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Sort array jobless input clocked numbers number elements content ascending order articles values interests guys buddies numbers more knowledge industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology NVidia Docker macOS