Python---冒泡排序、選擇排序

冒泡排序

依次輸入n個數,進行冒泡排序

冒泡排序法,即兩個相鄰的進行比較,比較之后換位置

def bubbleSort(arr):n = len(arr)for i in range(n):for j in range(0, n-i-1):if arr[j] > arr[j+1] :arr[j], arr[j+1] = arr[j+1], arr[j]arr=[]
n=int(input("請輸入要排序幾個數:"))
print("請依次輸入要排序的數:")
for i in range(n):arr.append(int(input()))bubbleSort(arr)print ("冒泡排序后的結果為:")
for i in range(len(arr)):print ("%d" %arr[i])

效果圖如下:
在這里插入圖片描述

選擇排序

依次輸入n個數,進行選擇排序

選擇排序法,即一個數依次與其后面的數進行比較,比較之后換位置

def selectSort(arr):n = len(arr)for i in range(n):for j in range(i, n):#第一個數依次與其后面的數進行比較if arr[i] > arr[j] :arr[i], arr[j] = arr[j], arr[i]arr=[]
n=int(input("請輸入要排序幾個數:"))
print("請依次輸入要排序的數:")
for i in range(n):arr.append(int(input()))selectSort(arr)print ("選擇排序后的結果為:")
for i in range(len(arr)):print ("%d" %arr[i])

效果圖如下:
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379180.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379180.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379180.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

react js 添加樣式_如何在React JS Application中添加圖像?

react js 添加樣式Hello! In this article, we will learn how to add images in React JS? I remember when I just started coding in React JS, I thought adding images would be done exactly as it is in HTML. I later realized that it was different. 你好&#xff0…

二叉樹(多路平衡搜索樹)-(代碼、分析、匯編)

目錄:代碼:分析:匯編:代碼: BTree.h #ifndef _BTREE_H_ #define _BTREE_H_#define BT_LEFT 0 //定義左子節點標識 #define BT_RIGHT 1 //定義右子節點標識typedef void BTree;//定義樹類型 typedef unsigned long lo…

window service服務安裝錯誤

今天按照園子里面的文章,弄了一個系統服務,可是一直裝不上去, 正在運行事務處理安裝。 正在開始安裝的“安裝”階段。查看日志文件的內容以獲得 D:\TecCreateSvc\TecJsCreateService.exe 程序集的進度。該文件位于 D:\TecCreateSvc\TecJsCre…

DM9000調試記錄

最近在調試DM9000,遇到了很多問題,在網上幾乎也能找到同樣的問題,但是答案千變萬化,弄的我這樣不行,那樣也不行。 1、遇到的第一個問題,網卡不識別,出現的調試信息就是: dm9000 dm90…

Python---二分法查找

輸入n個數&#xff0c;通過二分法查找該數的下標 def binarySearch(arr,value):m 0#開始n len(arr#最后)while m<n:mid(mn)//2#計算中間位置if valuearr[mid]:#查找成功&#xff0c;返回元素對應的位置return midelif value>arr[mid]:#在后面一半元素中繼續查找mmid1e…

Python datetime isocalendar()方法與示例

Python datetime.isocalendar()方法 (Python datetime.isocalendar() Method) datetime.isocalendar() method is used to manipulate objects of datetime class of module datetime. datetime.isocalendar()方法用于操作模塊datetime的datetime類的對象。 It uses a dateti…

ASP.NET 技術(附翻譯)

1.構建 ASP.NET 頁面ASP.NET 和ASP.NET結構ASP.NET 是微軟.NET framework整體的一部分, 它包含一組大量的編程用的類&#xff0c;滿足各種編程需要。 在下列的二個部分中, 你如何學會 ASP.NET 很適合的放在.NET framework, 和學會能在你的 ASP.NET 頁面中使用語言。.NET類庫假想…

SQL捕獲異常

原文地址 http://technet.microsoft.com/zh-cn/office/ms179296%28vsql.100%29在 Transact-SQL 中使用 TRY...CATCHTransact-SQL 代碼中的錯誤可使用 TRY…CATCH 構造處理&#xff0c;此功能類似于 Microsoft Visual C 和 Microsoft Visual C# 語言的異常處理功能。TRY…CATCH …

二叉樹遍歷(代碼,分析,匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; BTree.h BTree.c 二叉樹&#xff08;多路平衡搜索樹&#xff09; LinkQueue.h #ifndef _LINKQUEUE_H_ #define _LINKQUEUE_H_typedef void LinkQueue;//定義隊列類型LinkQueue* LinkQueu…

Java Vector insertElementAt()方法與示例

矢量類insertElementAt()方法 (Vector Class insertElementAt() method) insertElementAt() method is available in java.util package. insertElementAt()方法在java.util包中可用。 insertElementAt() method is used to set the given element (ele) at the given (indices…

Python---查找序列的最長遞增子序列

查找序列的最長遞增子序列 什么是序列的最長遞增子序列&#xff1f; 答&#xff1a;在一個數值序列中&#xff0c;找到一個子序列&#xff0c;使得這個子序列元素的數值依次遞增&#xff0c;并且這個子序列的長度盡可能地大。這就是所謂的最長遞增子序列 from itertools impo…

SendMessage和PostMessage

SendMessage 和 PostMessage 的區別 &#xff11;、首先是返回值意義的區別&#xff0c;我們先看一下 MSDN 里的聲明&#xff1a; LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);BOOL PostMessage( HWND hWnd…

ffmpeg-從mp4、flv、ts文件中提取264視頻流數據

ffmpeg-從mp4、flv、ts文件中提取264視頻流數據 main.c #include <stdio.h> #include <libavutil/log.h> #include <libavformat/avio.h> #include <libavformat/avformat.h>void proc(int need_to_annexb, char* in_file, char* out_file) {AVForma…

java timezone_Java TimeZone getDSTSavings()方法與示例

java timezoneTimeZone類的getDSTSavings()方法 (TimeZone Class getDSTSavings() method) getDSTSavings() method is available in java.util package. getDSTSavings()方法在java.util包中可用。 getDSTSavings() method is used to get the number of time differences in …

Photoshop 保存PNG格式交錯和不交錯有差別

1.PNG格式是由Netscape公司開發出來的格式&#xff0c;可以用于網絡圖像&#xff0c;但它不同于GIF格式圖像只能保存256色&#xff0c;PNG格式可以保存24位的真彩色圖像&#xff0c;并且支持透明背景和消除鋸齒邊緣的功能&#xff0c;可以在不失真的情況下壓縮保存圖像。但由于…

線索化二叉樹(代碼 、分析 、匯編)

目錄&#xff1a;代碼&#xff1a;分析&#xff1a;匯編&#xff1a;代碼&#xff1a; BTree.h BTree.c 二叉樹&#xff08;多路平衡搜索樹&#xff09; SeqList.h SeqList.c 順序表 main.c #include <stdio.h> #include <stdlib.h> #include "BTree.h&qu…

Python---尋找給定序列中相差最小的兩個數字

編寫函數&#xff0c;尋找給定序列中相差最小的兩個數字 def getTwoClosestElements(arr):#先進行排序&#xff0c;使得相鄰元素最接近#相差最小的元素必然相鄰seq sorted(arr)#先進行排序dif float(inf)#無窮大#遍歷所有元素&#xff0c;兩兩比較&#xff0c;比較相鄰元素的…

ubuntu 無線 共享 上網

配置DHCP服務器 使連接到此AP的電腦 自動獲取IP 1. 安裝軟件包&#xff1a;sudo apt-get install dhcp3-server2. 修改/etc/default/dhcp3-server配置文件INTERFACES"eth1" //eth1為無線網卡的名字3. 修改/etc/dhcp3/dhcpd.conf配置文件option domain-name-servers …

Java StringBuilder getChars()方法與示例

StringBuilder類的getChars()方法 (StringBuilder Class getChars() method) getChars() method is available in java.lang package. getChars()方法在java.lang包中可用。 getChars() method is used to copy all the characters from the given arguments (int src_st, int …

Python---利用蒙特.卡羅方法計算圓周率近似值

利用蒙特.卡羅方法計算圓周率近似值 什么是蒙特.卡羅方法&#xff1f; 答&#xff1a;蒙特卡羅方法是一種計算方法。原理是通過大量隨機樣本&#xff0c;去了解一個系統&#xff0c;進而得到所要計算的值。 正方形內部有一個相切的圓&#xff0c;它們的面積之比是π/4。 這里假…