Diễn đàn hỏi đáp học thuật - Download Tài Liệu Miễn Phí
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

Diễn đàn hỏi đáp học thuật - Download Tài Liệu Miễn PhíĐăng Nhập

VỮNG TIN - TIẾP BƯỚC - THÀNH CÔNG


descriptionThuật Toán Interchange Sort EmptyThuật Toán Interchange Sort

more_horiz
Code Interchange Sort (sắp xếp bằng đổi chỗ trực tiếp).

Code:


#include <stdio.h>
#include <conio.h>
#define max 100
void NhapMang(int A[], int &n)
{
   printf("Nhap n = ");
   scanf("%d",&n);
   for(int i = 0; i<n ; i++)
   {
      printf("Phan tu %d =",i);
      scanf("%d", &A[i]);
   }
}
void XuatMang(int A[], int n)
{
   printf("Mang sau khi sap xep la:");
   for(int i = 0; i<n ; i++)
      printf("%d ",A[i]);
}
void InterchangeSort(int A[], int n)
{
   for (int i=0; i<n-1; i++)
      for (int j=i+1; j<n; j++)
         if (A[i]>A[j])
         {
            int temp=A[i];
            A[i]=A[j];
            A[j]=temp;
         }
}
void main()
{
   int A[max],n;
   NhapMang(A,n);
   InterchangeSort(A,n);
   XuatMang(A,n);
   getch();
}


Demo thuật toán: http://www.mediafire.com/?nhygfymjzyz
Chương trình Demo này chạy từng bước của thuật toán, các bạn nhập khoảng 10 ~~> 20 phần tử rồi ngồi xem nó chạy nhé!
Nếu không hiểu, xem vài lần sẽ hiểu! Very Happy

descriptionThuật Toán Interchange Sort EmptyRe: Thuật Toán Interchange Sort

more_horiz
Thanks! Đây cũng là kỹ thuật sắp xếp khá hay. Bạn hãy đánh giá độ phức tạp của thuật toán này nhé.

descriptionThuật Toán Interchange Sort EmptyRe: Thuật Toán Interchange Sort

more_horiz
Thuật toán gồm n-1 bước, ở bước thứ i có n-i phép so sánh
=> có tổng cộng 1+2+...+n-1=n(n-1)/2 phép so sánh.
Vậy độ phức tạp của thuật toán này là O(n2).

descriptionThuật Toán Interchange Sort EmptyRe: Thuật Toán Interchange Sort

more_horiz
privacy_tip Permissions in this forum:
Bạn không có quyền trả lời bài viết
power_settings_newLogin to reply