起泡排序和簡單選擇排序小結

來源:互聯網
上載者:User

    起泡排序和簡單選擇排序都是一種很簡單的排序方法,它們的時間複雜度都為O(N2). 其中起泡排序是一種穩定的排序方法,而簡單選擇排序是一種不穩定的排序方法.

   這裡直接貼代碼

  

// 起泡排序和簡單選擇排序.cpp : 定義控制台應用程式的進入點。#include "stdafx.h"#include <iostream>using namespace std;//交換a與bvoid swap(int &a,int &b){  int temp=a;  a=b;  b=temp;}//冒泡排序void  BubbleSort(int * a,int len){    //進行len-1趟冒泡排序for(int i=1;i<len;i++){  bool exchage=false;//標記這一趟冒泡是否進行了資料交換,也就是標記是否排序完成  for(int j=0;j<len-i;j++)  {    if(a[j]>a[j+1]){          swap(a[j],a[j+1]);  exchage=true;}  }  if(!exchage)         break;}}//簡單選擇排序void SelectSort(int *a,int len){   //將前面n-1個位置的數選擇排好,最後一個自動排好了   for(int i=0;i<len-1;i++)   {     int min=i; for(int j=i+1;j<len;j++) { if(a[j]<a[min])   min=j; } if(i!=min) { swap(a[i],a[min]); }   }}//void printArray(int *a,int len){  for(int i=0;i<len;i++)  cout<<a[i]<<" ";  cout<<endl;}int _tmain(int argc, _TCHAR* argv[]){int a[]={49,38,65,97,76,13,27,49};int len=sizeof(a)/sizeof(int);printArray(a,len);    //BubbleSort(a,len);SelectSort(a,len);printArray(a,len);system("PAUSE");return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.