{"id":12,"date":"2022-10-09T17:11:29","date_gmt":"2022-10-09T09:11:29","guid":{"rendered":"https:\/\/zqy.ac.cn\/?p=12"},"modified":"2022-10-09T17:11:29","modified_gmt":"2022-10-09T09:11:29","slug":"%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95-%e6%8e%92%e5%ba%8f%e7%ae%97%e6%b3%95%e5%a4%a7%e5%85%a8","status":"publish","type":"post","link":"https:\/\/acosx.top\/?p=12","title":{"rendered":"\u6392\u5e8f\u7b97\u6cd5\u2014\u2014\u6392\u5e8f\u7b97\u6cd5\u5927\u5168"},"content":{"rendered":"<p>\u5305\u542b\u6240\u6709\u6392\u5e8f\u7b97\u6cd5-\u4f7f\u7528Github Copilot\u751f\u6210\u3002<\/p>\n<pre><code class=\"language-cpp\">#include&lt;bits\/stdc++.h&gt;\nusing namespace std;\n\nvoid InsertionSort(int arr[], int n)\n{\n    int i, j;\n    int get;\n    for (i = 1; i &lt; n; i++)\n    {\n        get = arr[i];\n        for (j = i - 1; j &gt;= 0; j--)\n        {\n            if (arr[j] &gt; get)\n                arr[j + 1] = arr[j];\n            else\n                break;\n        }\n        arr[j + 1] = get;\n    }\n}\n\nvoid BubbleSort(int arr[], int n)\n{\n    int i, j;\n    for (i = 0; i &lt; n-1; i++)\n\n        \/\/ Last i elements are already in place\n        for (j = 0; j &lt; n-i-1; j++)\n            if (arr[j] &gt; arr[j+1])\n                swap(arr[j], arr[j+1]);\n}\n\nvoid SelectionSort(int arr[], int n)\n{\n    int i, j;\n    int min;\n    for (i = 0; i &lt; n - 1; i++)\n    {\n        min = i;\n        for (j = i + 1; j &lt; n; j++)\n        {\n            if (arr[j] &lt; arr[min])\n                min = j;\n        }\n        if (min != i)\n            swap(arr[i], arr[min]);\n    }\n}\n\nvoid QuickSort(int arr[], int left, int right)\n{\n    int i = left, j = right;\n    int tmp;\n    int pivot = arr[(left + right) \/ 2];\n\n    \/* partition *\/\n    while (i &lt;= j) {\n        while (arr[i] &lt; pivot)\n            i++;\n        while (arr[j] &gt; pivot)\n            j--;\n        if (i &lt;= j) {\n            tmp = arr[i];\n            arr[i] = arr[j];\n            arr[j] = tmp;\n            i++;\n            j--;\n        }\n    };\n\n    \/* recursion *\/\n    if (left &lt; j)\n        QuickSort(arr, left, j);\n    if (i &lt; right)\n        QuickSort(arr, i, right);\n}\n\nvoid merge(int arr[], int l, int m, int r)\n{\n    int i, j, k;\n    int n1 = m - l + 1;\n    int n2 =  r - m;\n\n    \/* create temp arrays *\/\n    int L[n1], R[n2];\n\n    \/* Copy data to temp arrays L[] and R[] *\/\n    for (i = 0; i &lt; n1; i++)\n        L[i] = arr[l + i];\n    for (j = 0; j &lt; n2; j++)\n        R[j] = arr[m + 1+ j];\n\n    \/* Merge the temp arrays back into arr[l..r]*\/\n    i = 0; \/\/ Initial index of first subarray\n    j = 0; \/\/ Initial index of second subarray\n    k = l; \/\/ Initial index of merged subarray\n    while (i &lt; n1 &amp;&amp; j &lt; n2)\n    {\n        if (L[i] &lt;= R[j])\n        {\n            arr[k] = L[i];\n            i++;\n        }\n        else\n        {\n            arr[k] = R[j];\n            j++;\n        }\n        k++;\n    }\n\n    \/* Copy the remaining elements of L[], if there\n       are any *\/\n    while (i &lt; n1)\n    {\n        arr[k] = L[i];\n        i++;\n        k++;\n    }\n\n    \/* Copy the remaining elements of R[], if there\n       are any *\/\n    while (j &lt; n2)\n    {\n        arr[k] = R[j];\n        j++;\n        k++;\n    }\n}\n\nvoid mergeSort(int arr[], int l, int r)\n{\n    if (l &lt; r)\n    {\n        \/\/ Same as (l+r)\/2, but avoids overflow for\n        \/\/ large l and h\n        int m = l + (r - l) \/ 2;\n\n        \/\/ Sort first and second halves\n        mergeSort(arr, l, m);\n        mergeSort(arr, m + 1, r);\n\n        merge(arr, l, m, r);\n    }\n}\n\nvoid BucketSort(int arr[], int n)\n{\n    int i, j;\n    int count[n];\n    for (i = 0; i &lt; n; i++)\n        count[i] = 0;\n\n    for (i = 0; i &lt; n; i++)\n        (count[arr[i]])++;\n\n    for (i = 0, j = 0; i &lt; n; i++)\n        for (; count[i] &gt; 0; (count[i])--)\n            arr[j++] = i;\n}\n\nvoid CountSort(int arr[], int n)\n{\n    int i, j;\n    int max = arr[0];\n    int min = arr[0];\n    for (i = 1; i &lt; n; i++)\n    {\n        if (arr[i] &gt; max)\n            max = arr[i];\n        if (arr[i] &lt; min)\n            min = arr[i];\n    }\n    int range = max - min + 1;\n    int count[range];\n    int output[n];\n    for (i = 0; i &lt; range; i++)\n        count[i] = 0;\n\n    for (i = 0; i &lt; n; i++)\n        count[arr[i] - min]++;\n\n    for (i = 1; i &lt; range; i++)\n        count[i] += count[i - 1];\n\n    for (i = n - 1; i &gt;= 0; i--)\n    {\n        output[count[arr[i] - min] - 1] = arr[i];\n        count[arr[i] - min]--;\n    }\n\n    for (i = 0; i &lt; n; i++)\n        arr[i] = output[i];\n}\n\nvoid RadixSort(int arr[], int n)\n{\n    int i, j;\n    int max = arr[0];\n    for (i = 1; i &lt; n; i++)\n    {\n        if (arr[i] &gt; max)\n            max = arr[i];\n    }\n    for (int exp = 1; max \/ exp &gt; 0; exp *= 10)\n    {\n        int output[n];\n        int count[10] = { 0 };\n\n        for (i = 0; i &lt; n; i++)\n            count[(arr[i] \/ exp) % 10]++;\n\n        for (i = 1; i &lt; 10; i++)\n            count[i] += count[i - 1];\n\n        for (i = n - 1; i &gt;= 0; i--)\n        {\n            output[count[(arr[i] \/ exp) % 10] - 1] = arr[i];\n            count[(arr[i] \/ exp) % 10]--;\n        }\n\n        for (i = 0; i &lt; n; i++)\n            arr[i] = output[i];\n    }\n}\n\nvoid Heapify(int arr[], int n, int i)\n{\n    int largest = i;\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n\n    if (l &lt; n &amp;&amp; arr[l] &gt; arr[largest])\n        largest = l;\n\n    if (r &lt; n &amp;&amp; arr[r] &gt; arr[largest])\n        largest = r;\n\n    if (largest != i)\n    {\n        swap(arr[i], arr[largest]);\n        Heapify(arr, n, largest);\n    }\n}\n\nvoid Heap_sort(int arr[], int n)\n{\n    for (int i = n \/ 2 - 1; i &gt;= 0; i--)\n        Heapify(arr, n, i);\n\n    for (int i = n - 1; i &gt;= 0; i--)\n    {\n        swap(arr[0], arr[i]);\n        Heapify(arr, i, 0);\n    }\n}\n\nvoid ShellSort(int arr[], int n)\n{\n    for (int gap = n \/ 2; gap &gt; 0; gap \/= 2)\n    {\n        for (int i = gap; i &lt; n; i += 1)\n        {\n            int temp = arr[i];\n            int j;\n            for (j = i; j &gt;= gap &amp;&amp; arr[j - gap] &gt; temp; j -= gap)\n                arr[j] = arr[j - gap];\n            arr[j] = temp;\n        }\n    }\n}\n\nint main(){\n    int n;\n    cout &lt;&lt; &quot;Enter the number of elements: &quot;;\n    cin &gt;&gt; n;\n    int arr[n];\n    cout &lt;&lt; &quot;Enter the elements: &quot;;\n    for (int i = 0; i &lt; n; i++)\n        cin &gt;&gt; arr[i];\n    int choice;\n    cout &lt;&lt; &quot;Enter the choice of sorting algorithm: &quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;1. Bubble Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;2. Selection Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;3. Insertion Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;4. Quick Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;5. Merge Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;6. Bucket Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;7. Count Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;8. Radix Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;9. Heap Sort&quot; &lt;&lt; endl;\n    cout &lt;&lt; &quot;10. Shell Sort&quot; &lt;&lt; endl;\n    cin &gt;&gt; choice;\n    switch (choice)\n    {\n    case 1:\n        BubbleSort(arr, n);\n        break;\n    case 2:\n        SelectionSort(arr, n);\n        break;\n    case 3:\n        InsertionSort(arr, n);\n        break;\n    case 4:\n        QuickSort(arr, 0, n - 1);\n        break;\n    case 5:\n        mergeSort(arr, 0, n - 1);\n        break;\n    case 6:\n        BucketSort(arr, n);\n        break;\n    case 7:\n        CountSort(arr, n);\n        break;\n    case 8:\n        RadixSort(arr, n);\n        break;\n    case 9:\n        Heap_sort(arr, n);\n        break;\n    case 10:\n        ShellSort(arr, n);\n        break;\n    default:\n        cout &lt;&lt; &quot;Invalid choice&quot; &lt;&lt; endl;\n    }\n    cout &lt;&lt; &quot;Sorted array: &quot;;\n    for (int i = 0; i &lt; n; i++)\n        cout &lt;&lt; arr[i] &lt;&lt; &quot; &quot;;\n    cout &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5305\u542b\u6240\u6709\u6392\u5e8f\u7b97\u6cd5-\u4f7f\u7528Github Copilot\u751f\u6210\u3002 #include&lt;bits\/stdc++.h&#038;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-12","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":0,"href":"https:\/\/acosx.top\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/acosx.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}