Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the ad-inserter domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-includes/functions.php on line 6131

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rehub-framework domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-includes/functions.php on line 6131

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
[👨‍💻🇻🇳] Interchange Sort (đổi chỗ trực tiếp) - top1brand
Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

[👨‍💻🇻🇳] Interchange Sort (đổi chỗ trực tiếp)


Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74
class="post-inner post post-307915 type-post status-publish format-standard has-post-thumbnail hentry category-top1dev-no1dev category-top1index-top1list-top1world category-top1labs-no1labs category-top1vietnam-no1vietnam tag-no1dev tag-no1labs tag-top1dev tag-top1labs tag-cho tag-doi tag-interchange tag-no1vietnam tag-sort tag-tiep tag-top1index tag-top1list tag-top1vietnam tag-truc" id="post-307915"> > .aff-auto-link, .aff-auto-url { color: #0073aa !important; text-decoration: underline dashed #0073aa !important; font-weight: 500; cursor: pointer; transition: all 0.2s; } .aff-auto-link:hover, .aff-auto-url:hover { color: #d32f2f !important; text-decoration: underline solid #d32f2f !important; } .aff-trending-box { margin: 25px 0; padding: 15px; border: 1px dashed #ddd; border-left: 4px solid #d32f2f; background: #f9f9f9; border-radius: 4px; } .aff-trending-box h4 { margin-top: 0; font-size: 16px; color: #333; } .aff-trending-box ul { list-style: none !important; padding: 0 !important; margin: 10px 0 0 !important; display: flex; flex-wrap: wrap; gap: 10px; } .aff-trending-box li a { background: #fff; border: 1px solid #ccc; padding: 5px 12px; border-radius: 20px; font-size: 13px; color: #555 !important; text-decoration: none !important; display: inline-block; transition: 0.3s; } .aff-trending-box li a:hover { background: #d32f2f; color: #fff !important; border-color: #d32f2f; }

Để sắp xếp một dãy, có nhiều cách làm khác nhau. Trong bài viết này mình sẽ nói về Interchange Sort, hay còn gọi là thuật toán sắp xếp đổi chỗ trực tiếp.

Ý tưởng của thuật toán này là bắt cặp tất cả các phần tử trong dãy cần sắp xếp và đổi chỗ hai phần tử trong cặp nếu chúng nghịch thế (không thỏa điều kiện thứ tự).

Để bắt cặp tất cả các phần tử trong dãy, ta dùng 2 vòng lặp. Vòng lặp ngoài sẽ chạy từ đầu dãy đến phần tử kế cuối. Vòng lặp trong sẽ chạy từ phần tử tiếp theo của vị trí đang xét ở vòng lặp ngoài. Mỗi lần xét ta sẽ so sánh 2 phần tử trong cặp. Nếu chúng nghịch thế thì sẽ hoán đổi vị trí của chúng.

Ta thấy sau mỗi lần duyệt ở vòng lặp ngoài ta sẽ được phần tử đầu tiên đứng đúng thứ tự yêu cầu. Cứ thực hiện đến hết dãy ta sẽ được một dãy đã sắp xếp.

Mã dưới đây được cài đặt theo ngôn ngữ C/C++ với yêu cầu sắp xếp dãy tăng dần.

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;
         }
}

Số phép so sánh của thuật toán này không đổi, không phụ thuộc tình trạng ban đầu của dãy. Phần tử thứ i được so sánh với n-i phần tử còn lại nên có thể ước lượng số phép so sánh bằng: $(n-1) + (n-2) + … + 1 = n(n – 1)/2$.

Tuy nhiên số phép hoán vị (3 phép gán) lại phụ thuộc tình trạng ban đầu. Trong trường hợp tốt nhất, tức dãy đã được sắp xếp hoàn toàn thì không cần phải hoán vị, nên số phép hoán vị là: 0.
Trường hợp xấu nhất, tức dãy có thứ tự ngược với yêu cầu. Mỗi lần so sánh ta lại phải hoán vị nên số phép hoán vị là: $n(n – 1)/2$.

Do vậy độ phức tạp của thuật toán này là: $O(n^2)$.

#Interchange #Sort #đổi #chỗ #trực #tiếp

[bsa_pro_ad_space id=2]

Notice: Trying to get property 'post_type' of non-object in /www/wwwroot/hdd-vatly/re_mysql.top1index-top1list.com/wp-content/mu-plugins/post-media-only.php on line 74

 ⭐ ☀ ⚡ 
Born to keep your brand's great stories forever!Bring your brand to the World !

Zalo Viber Telegram WhatsApp Call
top1brand
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart