

{"id":135438,"date":"2024-05-27T10:28:19","date_gmt":"2024-05-27T04:58:19","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=135438"},"modified":"2024-05-27T10:28:19","modified_gmt":"2024-05-27T04:58:19","slug":"bubble-sort-in-data-structures-using-cpp","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/","title":{"rendered":"Bubble Sort in Data Structures using C++"},"content":{"rendered":"<h3>Program 1<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/\/ Program for Bubble sort\r\n#include&lt;iostream&gt;\r\n#define clrscr() system(\"cls\")\r\nusing namespace std;\r\nclass Sort\r\n{  \r\n    public:\r\n       void bubblesort(int ar[],int n)\r\n       {\r\n              int i,j,temp;\r\n        for(i=0;i&lt;n-1;i++)\r\n        {\r\n           for(j=0;j&lt;n-i-1;j++)\r\n           {\r\n                if(ar[j]&gt;ar[j+1])\r\n                {\r\n                      temp=ar[j];\r\n                      ar[j]=ar[j+1];\r\n                      ar[j+1]=temp;\r\n                } \r\n           }\r\n       }\r\n       cout&lt;&lt;\"\\n Sorted elements:\\n\";\r\n       for(i=0;i&lt;n;i++) \r\n       cout&lt;&lt;ar[i]&lt;&lt;\"\\n\";\r\n   }\r\n};\r\n\r\nint main()\r\n{\r\n    int a[500],n,i,j,temp;\r\n    clrscr();\r\n    xyz:cout&lt;&lt;\"\\nEnter the limit of array\";\r\n    cin&gt;&gt;n;\r\n    if(n&lt;0 || n&gt;500)\r\n    {\r\n        cout&lt;&lt;\"\\nInvalid limit pls enter again...\\n\";\r\n        goto xyz;\r\n    }\r\n    else\r\n    {\r\n        cout&lt;&lt;\"\\nEnter the element in array\\n\";\r\n        for(i=0;i&lt;n;i++)\r\n        cin&gt;&gt;a[i];\r\n        Sort S;\r\n        S.bubblesort(a,n);\r\n    }\r\n   return 0;\r\n}         \r\n\/\/         \/\/ Bubble sort\r\n\/\/         for(i=0;i&lt;n-1;i++)\r\n\/\/         {\r\n\/\/            for(j=0;j&lt;n-i-1;j++)\r\n\/\/            {\r\n\/\/                 if(a[j]&gt;a[j+1])\r\n\/\/                 {\r\n\/\/                       temp=a[j];\r\n\/\/                       a[j]=a[j+1];\r\n\/\/                       a[j+1]=temp;\r\n\/\/                 } \r\n\/\/            }\r\n\/\/         }\r\n\/\/        cout&lt;&lt;\"\\n Sorted elements:\\n\";\r\n\/\/        for(i=0;i&lt;n;i++) \r\n\/\/        cout&lt;&lt;a[i]&lt;&lt;\"\\n\";\r\n\/\/     }\r\n\/\/     return 0;\r\n\/\/ }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Program 1 \/\/ Program for Bubble sort #include&lt;iostream&gt; #define clrscr() system(&#8220;cls&#8221;) using namespace std; class Sort { public: void bubblesort(int ar[],int n) { int i,j,temp; for(i=0;i&lt;n-1;i++) { for(j=0;j&lt;n-i-1;j++) { if(ar[j]&gt;ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp;&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31449],"tags":[24493,32288,24331,32249,32251,32250,32279],"class_list":["post-135438","post","type-post","status-publish","format-standard","hentry","category-dsa-cpp-tutorials","tag-bubble-sort","tag-bubble-sort-in-dsa-using-c","tag-data-structures","tag-dsa-using-c","tag-dsa-using-c-practical","tag-dsa-using-c-program","tag-dsa-with-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Bubble Sort in Data Structures using C++ - DataFlair<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Bubble Sort in Data Structures using C++ - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Program 1 \/\/ Program for Bubble sort #include&lt;iostream&gt; #define clrscr() system(&quot;cls&quot;) using namespace std; class Sort { public: void bubblesort(int ar[],int n) { int i,j,temp; for(i=0;i&lt;n-1;i++) { for(j=0;j&lt;n-i-1;j++) { if(ar[j]&gt;ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp;&#046;&#046;&#046;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-05-27T04:58:19+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Bubble Sort in Data Structures using C++ - DataFlair","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:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/","og_locale":"en_US","og_type":"article","og_title":"Bubble Sort in Data Structures using C++ - DataFlair","og_description":"Program 1 \/\/ Program for Bubble sort #include&lt;iostream&gt; #define clrscr() system(\"cls\") using namespace std; class Sort { public: void bubblesort(int ar[],int n) { int i,j,temp; for(i=0;i&lt;n-1;i++) { for(j=0;j&lt;n-i-1;j++) { if(ar[j]&gt;ar[j+1]) { temp=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temp;&#46;&#46;&#46;","og_url":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-05-27T04:58:19+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Bubble Sort in Data Structures using C++","datePublished":"2024-05-27T04:58:19+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/"},"wordCount":9,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"keywords":["Bubble Sort","bubble sort in dsa using c++","Data Structures","dsa using c++","dsa using c++ practical","dsa using c++ program","dsa with c++"],"articleSection":["DSA using C++ Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/","url":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/","name":"Bubble Sort in Data Structures using C++ - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"datePublished":"2024-05-27T04:58:19+00:00","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/bubble-sort-in-data-structures-using-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"DSA using C++ Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/dsa-cpp-tutorials\/"},{"@type":"ListItem","position":3,"name":"Bubble Sort in Data Structures using C++"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/135438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=135438"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/135438\/revisions"}],"predecessor-version":[{"id":142578,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/135438\/revisions\/142578"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=135438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=135438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=135438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}