{"id":12329,"date":"2024-03-14T15:36:06","date_gmt":"2024-03-14T15:36:06","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/"},"modified":"2025-08-05T00:13:33","modified_gmt":"2025-08-05T00:13:33","slug":"what-is-the-method-for-sorting-arrays-in-c","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/","title":{"rendered":"C++ Array Sorting Methods"},"content":{"rendered":"<p>There are multiple sorting methods for arrays in C++, here are some common ones:<\/p>\n<ol>\n<li>Bubble Sort: By comparing adjacent elements and continuously swapping positions, the larger values gradually &#8220;bubble&#8221; towards the end of the array.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">bubbleSort<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> arr[], <span class=\"hljs-type\">int<\/span> n)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; n<span class=\"hljs-number\">-1<\/span>; i++) {\r\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> j = <span class=\"hljs-number\">0<\/span>; j &lt; n-i<span class=\"hljs-number\">-1<\/span>; j++) {\r\n            <span class=\"hljs-keyword\">if<\/span> (arr[j] &gt; arr[j+<span class=\"hljs-number\">1<\/span>]) {\r\n                <span class=\"hljs-type\">int<\/span> temp = arr[j];\r\n                arr[j] = arr[j+<span class=\"hljs-number\">1<\/span>];\r\n                arr[j+<span class=\"hljs-number\">1<\/span>] = temp;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Selection Sort: Each time, the algorithm finds the smallest (or largest) element from the unsorted portion and swaps it with the first element of the unsorted portion.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">selectionSort<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> arr[], <span class=\"hljs-type\">int<\/span> n)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> i = <span class=\"hljs-number\">0<\/span>; i &lt; n<span class=\"hljs-number\">-1<\/span>; i++) {\r\n        <span class=\"hljs-type\">int<\/span> minIndex = i;\r\n        <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> j = i+<span class=\"hljs-number\">1<\/span>; j &lt; n; j++) {\r\n            <span class=\"hljs-keyword\">if<\/span> (arr[j] &lt; arr[minIndex]) {\r\n                minIndex = j;\r\n            }\r\n        }\r\n        <span class=\"hljs-type\">int<\/span> temp = arr[i];\r\n        arr[i] = arr[minIndex];\r\n        arr[minIndex] = temp;\r\n    }\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Insertion Sort: Dividing the array into a sorted portion and an unsorted portion, each time inserting the first element of the unsorted portion into the correct position of the sorted portion.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">insertionSort<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> arr[], <span class=\"hljs-type\">int<\/span> n)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> i = <span class=\"hljs-number\">1<\/span>; i &lt; n; i++) {\r\n        <span class=\"hljs-type\">int<\/span> key = arr[i];\r\n        <span class=\"hljs-type\">int<\/span> j = i - <span class=\"hljs-number\">1<\/span>;\r\n        <span class=\"hljs-keyword\">while<\/span> (j &gt;= <span class=\"hljs-number\">0<\/span> &amp;&amp; arr[j] &gt; key) {\r\n            arr[j+<span class=\"hljs-number\">1<\/span>] = arr[j];\r\n            j--;\r\n        }\r\n        arr[j+<span class=\"hljs-number\">1<\/span>] = key;\r\n    }\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Quick Sort: Select a pivot element, partition the array into two subarrays with elements on the left less than or equal to the pivot and elements on the right greater than the pivot, recursively apply quick sort to the subarrays.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-function\"><span class=\"hljs-type\">int<\/span> <span class=\"hljs-title\">partition<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> arr[], <span class=\"hljs-type\">int<\/span> low, <span class=\"hljs-type\">int<\/span> high)<\/span> <\/span>{\r\n    <span class=\"hljs-type\">int<\/span> pivot = arr[high];\r\n    <span class=\"hljs-type\">int<\/span> i = low - <span class=\"hljs-number\">1<\/span>;\r\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-type\">int<\/span> j = low; j &lt;= high<span class=\"hljs-number\">-1<\/span>; j++) {\r\n        <span class=\"hljs-keyword\">if<\/span> (arr[j] &lt; pivot) {\r\n            i++;\r\n            <span class=\"hljs-type\">int<\/span> temp = arr[i];\r\n            arr[i] = arr[j];\r\n            arr[j] = temp;\r\n        }\r\n    }\r\n    <span class=\"hljs-type\">int<\/span> temp = arr[i+<span class=\"hljs-number\">1<\/span>];\r\n    arr[i+<span class=\"hljs-number\">1<\/span>] = arr[high];\r\n    arr[high] = temp;\r\n    <span class=\"hljs-keyword\">return<\/span> i + <span class=\"hljs-number\">1<\/span>;\r\n}\r\n\r\n<span class=\"hljs-function\"><span class=\"hljs-type\">void<\/span> <span class=\"hljs-title\">quickSort<\/span><span class=\"hljs-params\">(<span class=\"hljs-type\">int<\/span> arr[], <span class=\"hljs-type\">int<\/span> low, <span class=\"hljs-type\">int<\/span> high)<\/span> <\/span>{\r\n    <span class=\"hljs-keyword\">if<\/span> (low &lt; high) {\r\n        <span class=\"hljs-type\">int<\/span> pi = <span class=\"hljs-built_in\">partition<\/span>(arr, low, high);\r\n        <span class=\"hljs-built_in\">quickSort<\/span>(arr, low, pi<span class=\"hljs-number\">-1<\/span>);\r\n        <span class=\"hljs-built_in\">quickSort<\/span>(arr, pi+<span class=\"hljs-number\">1<\/span>, high);\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>These are just some common sorting algorithms, and there are implementations of other sorting algorithms in C++. Choosing the appropriate sorting algorithm based on the actual situation and needs can both improve sorting efficiency and reduce resource consumption.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are multiple sorting methods for arrays in C++, here are some common ones: Bubble Sort: By comparing adjacent elements and continuously swapping positions, the larger values gradually &#8220;bubble&#8221; towards the end of the array. void bubbleSort(int arr[], int n) { for (int i = 0; i &lt; n-1; i++) { for (int j = [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[7899,6257,274,11914,16110],"class_list":["post-12329","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-algorithms","tag-bubble-sort","tag-c","tag-selection-sort","tag-sorting-arrays"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++ Array Sorting Methods - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort &amp; optimized algorithms.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Array Sorting Methods\" \/>\n<meta property=\"og:description\" content=\"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort &amp; optimized algorithms.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T15:36:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-08-05T00:13:33+00:00\" \/>\n<meta name=\"author\" content=\"Ava Mitchell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ava Mitchell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\"},\"author\":{\"name\":\"Ava Mitchell\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\"},\"headline\":\"C++ Array Sorting Methods\",\"datePublished\":\"2024-03-14T15:36:06+00:00\",\"dateModified\":\"2025-08-05T00:13:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\"},\"wordCount\":175,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"algorithms\",\"Bubble Sort\",\"c#\",\"selection sort\",\"sorting arrays\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\",\"name\":\"C++ Array Sorting Methods - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T15:36:06+00:00\",\"dateModified\":\"2025-08-05T00:13:33+00:00\",\"description\":\"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort & optimized algorithms.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Array Sorting Methods\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64\",\"name\":\"Ava Mitchell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g\",\"caption\":\"Ava Mitchell\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C++ Array Sorting Methods - Blog - Silicon Cloud","description":"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort & optimized algorithms.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/","og_locale":"en_US","og_type":"article","og_title":"C++ Array Sorting Methods","og_description":"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort & optimized algorithms.","og_url":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T15:36:06+00:00","article_modified_time":"2025-08-05T00:13:33+00:00","author":"Ava Mitchell","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Ava Mitchell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/"},"author":{"name":"Ava Mitchell","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64"},"headline":"C++ Array Sorting Methods","datePublished":"2024-03-14T15:36:06+00:00","dateModified":"2025-08-05T00:13:33+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/"},"wordCount":175,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["algorithms","Bubble Sort","c#","selection sort","sorting arrays"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/","url":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/","name":"C++ Array Sorting Methods - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T15:36:06+00:00","dateModified":"2025-08-05T00:13:33+00:00","description":"Learn top C++ array sorting methods with code examples: Bubble Sort, Selection Sort & optimized algorithms.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/what-is-the-method-for-sorting-arrays-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C++ Array Sorting Methods"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/a3e2658c2cb9fb2be95ae0a8861f4a64","name":"Ava Mitchell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/15c63cd0564b4a2e07d611bcdffa296f6ea80e8db07c3091f43a84010514899d?s=96&d=mm&r=g","caption":"Ava Mitchell"},"url":"https:\/\/www.silicloud.com\/blog\/author\/avamitchell\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=12329"}],"version-history":[{"count":2,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12329\/revisions"}],"predecessor-version":[{"id":156120,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/12329\/revisions\/156120"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=12329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=12329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=12329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}