This web page requires JavaScript to be enabled.

JavaScript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.

How to enable JavaScript?

Sắp xếp mảng tăng dần

10cth1-2 August 1, 2011 Nam Le 0

C1:
[codes=c]#include "stdio.h"
void sapxep(int a[],int n);
void nhap(int a[],int &n);
void main()
{
  int a[100];
  int n;
  nhap(a,n);
  sapxep(a,n);
  printf("Mang sau khi sap xep la: ");
  for(int i=0;i<n;i++)
    printf("%4d",a[i]);
  printf("\n");
}
void nhap(int a[],int &n)
{
  printf("nhap so phan tu: ");
  scanf("%d",&n);
  for(int i=0; i<n;i++)
  {
    printf("nhap a[%d]: ",i);
    scanf("%d",&a[i]);
  }
}

void sapxep(int a[],int n)
{
  int tam;
  for(int i=0;i<n-1;i++)
  {
    for(int k=i+1;k<n;k++)
    {
      if(a[i]<a[k])
      {
        
        tam=a[i];
        a[i]=a[k];
        a[k]=tam;
      }
    }
  }
}[/codes]

c2:
[codes=c]int hoanvi (int &a, int &b)
{
  int tam;
  tam = a;
  a=b;
  b=tam;
}

void sapxep_tang (int a[], int n)
{
  for (int i=0, j<n-1; i<j;)
    {
      if (a[i]>a[j])
        hoanvi(a[i], a[j])
    }
}[/codes]
Các bạn nhớ để í cái dòng for (int i=0, j < n-1 ; i < j ;) nhe

p/s:
[codes=c]for (int i=0, j<n-1; i<j;) [/codes]
là thay thế của
[codes=c]
for (int i=0; i<n-1; i++)
    for (int j=i+1; j<n; j++)
[/codes]


Last modified on March 31st, 2021 at 1:17 am

Nam Le
lequocnam



0 responds

Leave a Reply

Your email address will not be published. Required fields are marked *