fbpx
วิกิพีเดีย

การจัดเรียงแบบลูป

Cycle Sort เป็นการจัดเรียงตัวเลขตามลำดับ โดยเอาตัวเลขในลำดับแรกเทียบกับตัวเลขลำดับถัดไปเรื่อยๆ จนตัวเลขตัวแรกที่เทียบตรงกับลำดับ เช่น ตัวเลขตัวแรกลำดับคือ5 เทียบกับ ตำแหน่งต่อไปเรื่อยจนถึงตำแหน่งที่5 ก็จะทำการสลับตำแหน่งให้ตัวเลขในตำแหน่งที่5นั้นไปอยู่ตำแหน่งแรก และเอาเลข5จากตำแหน่งแรกไปอยู่ตำแหน่งที่5 และทำเหมือนเดิมซ้ำไปเรื่อยๆจนทุกตัวอยู่ในตำแหน่ง

การจัดเรียงแบบวนลูป

Coding

def cycleSort(array): for cycleStart in range(0, len(array) - 1): item = array[cycleStart]  pos = cycleStart for i in range(cycleStart + 1, len(array)): if array[i] < item: pos += 1 if pos == cycleStart: continue while pos != cycleStart: pos = cycleStart for i in range(cycleStart + 1, len(array)): if array[i] < item:  pos += 1 while item == array[pos]: pos += 1 array[pos],item = item, array[pos] return(array) 

อ้างอิง

Nishant Singh;geeksforgeeks.cycle sort

การจ, ดเร, ยงแบบล, cycle, sort, เป, นการจ, ดเร, ยงต, วเลขตามลำด, โดยเอาต, วเลขในลำด, บแรกเท, ยบก, บต, วเลขลำด, บถ, ดไปเร, อยๆ, จนต, วเลขต, วแรกท, เท, ยบตรงก, บลำด, เช, วเลขต, วแรกลำด, บค, อ5, เท, ยบก, ตำแหน, งต, อไปเร, อยจนถ, งตำแหน, งท, จะทำการสล, บตำแหน, งให. Cycle Sort epnkarcderiyngtwelkhtamladb odyexatwelkhinladbaerkethiybkbtwelkhladbthdiperuxy cntwelkhtwaerkthiethiybtrngkbladb echn twelkhtwaerkladbkhux5 ethiybkb taaehnngtxiperuxycnthungtaaehnngthi5 kcathakarslbtaaehnngihtwelkhintaaehnngthi5nnipxyutaaehnngaerk aelaexaelkh5caktaaehnngaerkipxyutaaehnngthi5 aelathaehmuxnedimsaiperuxycnthuktwxyuintaaehnng karcderiyngaebbwnlupCoding aekikhdef cycleSort array for cycleStart in range 0 len array 1 item array cycleStart pos cycleStart for i in range cycleStart 1 len array if array i lt item pos 1 if pos cycleStart continue while pos cycleStart pos cycleStart for i in range cycleStart 1 len array if array i lt item pos 1 while item array pos pos 1 array pos item item array pos return array xangxing aekikhNishant Singh geeksforgeeks cycle sortekhathungcak https th wikipedia org w index php title karcderiyngaebblup amp oldid 9185617, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

, อ่าน, ดาวน์โหลด, ฟรี, ดาวน์โหลดฟรี, mp3, วิดีโอ, mp4, 3gp, jpg, jpeg, gif, png, รูปภาพ, เพลง, เพลง, หนัง, หนังสือ, เกม, เกม