Académique Documents
Professionnel Documents
Culture Documents
. 1776
?
:
insertion sort
selection sort
bubble sort
quicksort
Insertion sort
Insertion sort C++
void insertionsort(int a[], int n){
int j,t;
for(int i=1; i<n; i++){
j=i;
t=a[j];
while (j>0 && a[j-1] > t){
a[j]=a[j-1];
j--;
}
a[j]=t;
}}
Selection sort
Selection sort C++
void selectionsort(int a[], int n){
int min,i,temp,j;
for(i=0;i<n-1;i++){
min=i;
for(j=i+1;j<n;j++){
if(a[min]>a[j])
min=j;}
if (min!=i){
temp=a[i];
a[i]=a[min];
a[min]=temp;}
}}
Bubble sort
Bubble sort C++
void bubblesort(int a[], int n){
int i,j,temp,smeni;
for(i=n-1;i>=0;i--){
smeni=0;
for(j=0;j<i;j++){
if(a[j+1]<a[j]){
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
smeni++;}}
if (smeni==0){
break;}}}
Quicksort
int partition(int input[], int p, int r)
{int pivot = input[(p+r)/2];
while ( p < r )
{
while ( input[p] < pivot )
p++;
while ( input[r] > pivot )
r--;
if ( input[p] == input[r] )
p++;
else if ( p < r )
{
int tmp = input[p];
input[p] = input[r];
input[r] = tmp;
Insertion sort
()
c1
j=i;
c2
n-1
t=a[j];
c3
n-1
c4
a[j]=a[j-1];
c5
j--;}
c6
a[j]=t;}
c7
n-1
, ,
j=1,2,3.,n, while ,
tj=1.
,
. j=1,2,3,,n, while
, tj=j.
:
Insertion sort T(n)=n
Insertion sort T(n)=n2
Bubble sort
(n)
(n2)
(n2)
Insertion sort
(n)
(n2)
(n2)
Selection sort
(n2)
(n2)
(n2)
Quicksort
(n log n)
(n log n)
(n2)