首页 > > 详细

C辅导min sort, bubble sort, insertion sort, quick sort Sorting辅导C编程

,min sort, bubble sort, insertion sort, quick sortmerge sort.
 

Introduction

Putting items into a sorted order is one of the most common tasks in Computer Science, and so as a result there are myriad library routines that will do this task for youbut that does not absolve you of the obligation of understanding how it is done, and in fact it behooves you to understand the various algorithms in order to make wise choices.

The best that can be accomplished (the lower bound) for sorting using comparisons is (n log n) where n is the number is elements to be sorted. If the universe of elements to be sorted is limited (small), then we can do better using a Count Sort, where is O(n), where we count the number of occurrences of each element in an array, or a Radix Sort, which is also O(n) with a constant proportional to the maximum number of digits in the numbers being sorted.

What is this O and stuff? It’s how we talk about the execution time (or space) of programs. We will discuss it in class, and you will see it again in CMPS 101.

min-Sort

Perhaps the simplest sorting method is to look for the smallest element (the minimum) and move it to the top. We could do this by making a copy of that element, and then shifting everything down by one, and then placing the copy in the top slot. But that seems silly, why move all of those elements? Let’s just exchange (swap) the top element with the smallest element.

At this point what do we know? We know that the smallest element is at the top. Since that is true and it will not change (we call this an invariant), we can forget about it and move on. Let’s consider the second element:

Why not just do what we did the first time? If we do that, then what do we know? We now know that the top (first) element is the smallest, and the second element is the second smallest (or the same, if there are duplicates). We can repeat this for each element in the array, in succession, up to but not including the last element. By the method of induction, we can show that the array is sorted.

Why do we not need to concern ourselves with the last element? The answer is that if it were not the smaller element when we were at the last step, then it was exchanged with the penultimate element, and thus must necessarily be the largest (and consequently, last) element in the array.

To get you started, here is the code for minSort. Notice that it is composed of two functions: minIndex which finds the location of the smallest element, and minSort which actually performs the sorting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// minIndex : find the index of the least element.

uint32_t minIndex(uint32_t a[], uint32_t first, uint32_t last)
#123;
uint32_t smallest = first; // Assume the first is the least
for (uint32_t i = first; i lt; last; i += 1)
#123;
smallest = a[i] lt; a[smallest] ? i : smallest;
#125;
return smallest;
#125;

// minSort : sort by repeatedly finding the least element.

void minSort(uint32_t a[], uint32_t length)
#123;

for (uint32_t i = 0; i lt; length - 1; i += 1)
#123;
uint32_t smal㹥㱳‽⼠㹭㱩⁥㵩∬≧㹴ㅨ㈩㰻⼼㹮㰾⁲⼠㸯㰾㵳∽≥㸢ㄾ㌠㰠⼠㹡㱮⽡㹳㱳⼽㹹൷੯⁲⁤•‾⁩⁦‼ ⁳⁰⁡㰾⼠㹭ൡ੬⁥⁳⁴†‡‽†⁩
†‼㱳㵳∽≭㹥൮ੴ•‾  †⁉⁴‧⁳†⁳⁩㱬㹴㱯⁰㴠≹≥㹬‼⠯䄾‼㩳⁰⁳†⤲㰳⼻㹡㱮⁢⽲㸠㰯⁡㵳≳≮㹥•‾†㴠†⠨䅡⥛㱳⽭㹳㱴†⽡㹛㱩 㴼≢∼㹳⁰⁡㱳⼽㹥㰢†⼠㸠㰣‼㴾∼∾㸼⁳⁰⁡•㵬⁩㰠⼣㸼㰯⁡⽮㸾㰼‾㴠≣∽㸢⁩⁥†⁳㵰⁡ㅮ†⁡⁳ⴽㄢ⁥㱲⽮㹡㱮‼⼯㹳㱰⁢㵳≰≬㹡⁳⁳‽•⁩•䄾ⴲㄵ‼㭰⁡䅮⁲㰯⽰㸊㰠†⼠㸠㰠†㴍∊∠㸠††‼ ⁴⁲‾‍⠠䄼ⵢㅯⱹ‾䄍⥴㱡⽢㸊㰼‾⼍㸊㰼⁡㵴≨≭㹥†⁣⁰⁥⁸㵩㰿⼠㹫㱥⁩⽳㸠㱴㵴≨≲㸠⁰‬††㰠⽳㹨㱡⁴⽨㹥㰠†㵥≣∠㹬⁥⁧⁴⁩㰮⼠㸠㱷⽤㸠㱳⁴㵩≣≴㹨⁡⁴†⁴⁥㴠⁳⁲⵴†ㅩ㱳⼠㸬㰠⁴⼠㹷㱥⁤㵯≯≲㹴⁨⁥††㱰⼠㹥㱥⁨⽡㹴㰠㵭≡≯㸠⁉㰠⽷㹷㱥⽯㹫㰠⽴㹥ഠ੷⁥†⁦⁩⁤†⁹⁥⁴†⁡㱮⽯㹥൲ਠ⁦⁲†⁰㰮⼠㹩൳ਠ⁰㰠⽩㹣൵ੴ㱥⽤㹴ഠਭ㰠⽦㹲൳ੴ㰩㍴⁩㵳∬䤠ⴠ≤㸠䥯‮㱷⽥㍣㹡൬੬㰠㹩䥮⁰⁥⁴⵴†Ⱐ⁴⁥⁧⁰⁡†⁡⁵⁴⁩⁣⁴⁓⁴†⸩‮䤼⁰†⁵⁩Ⱐ⁷†⁩⁥⁹⁡⁷⹷⁥䔠Ⱐ⁴⁥⁡⁥⁵⁥⁵⁩†⁵†⁦⁢††⁥⁡⁦⁴⸠㱩⁃⼠㹡൬੷㱡⁳㵲∠†≬㹵㱥⼠㹩൳ਠ㱩㹥൮੩⁥⁴†㱦㹥ഠੳ⁷⁡⁰†⁰⁥⁲㱡㹯൮ਮ†⁁†⁣⁥⁶⁥⁲†⁰⁥㱲㵴∠≤㹶ൡ੮⁴⁡⁧⁥†⁦†⁴⁨⁥†㱭㹯㰠㴠≩≵㹭ㅥ㱮⽴㹮㰮⁲⼠㸯㰾⁩㵣≬∢㹨㉩㱧⽨㹴㰠‾⼼㸯㱦⁥㵡≢∊㸠㌠㰠⼠㹤㱹 ⼠㸠㰠†㴊∠∠㸠㐠㰠⼠㹤㰠⁡⽳㹳㰽⁴㴊∠∠㸠㔠㰠⼠㹲㱥⁳⽰㹡㱮⁳㵮≥∯㹳㙰㱡⽮㸠㰯⁳⽰㹡㱮⁳㵮≥∯㹳㝰㱡⽮㸠㰯⁳⽰㹡㱮⁳㵮≥∯㹳㡰㱡⽮㸠㰯⁳⽰㹡㱮⁳㵮≥∯㹳㥰㱡⽮㸠㰯⁳⽰㹡㱮⁳㵮≥∯㹳ㅰち㱮⼾㸯㰾⁰⽲㹥㰾†㴠∠≴㹤ㄾㄍ㰊⼠㸠㰠†⼠㸠㰼⽴㹬ൡੳ⁳‽•⁣⁤⁥•‾‍ †㰠⼠㸠ഠਠ††††‼⁰⁲⁥‾‼⁳⁰㱡⁣㴢≬∾㸼൳ੰ⁡†⁣⁡⁳⁳‽•⁥㱴㸣㰠㵳∽≡㸭⁩⡉䅎⁓㩔⁒⁔‼⁳⥢㱲⼠㹰㱡⁣⽬㹡㱳⁩㵳≰≬㹡⁳⁳⁥⁡㴢‾ㄣ†⁰⡳䅳⤽㰢⽭㹫㱥⽲㹤㰢⁩㵰≡≗㹁⁐
⁸‬†㴣‱䄲㱵⽩㹟㱴†⼽㸠㱸†㴠≹∠㸻†※†⁶㵥⁃⁵⵮⁴ㄠ㰫⼽㸣㰱※⼼㸯㱳‼㴾∼∾㸼⁳⁰⁡⁳•㭮㵥•〾‼䄠⁳㬢㰢⼾㹰㱡⁣⽬㹡㱳⁥㵹≷∾㹥⁳⁥‼ ⁳䅰‾⬼ ㅳ⁡㵮‾䄼㱡⽮㸠㰯⁳⽰㹡㱮⁳㵮≥≰㹡†⁣⁡⁳‽㴢⁴ⵡ•ㄾ㰣⼠㹮㰠⁡⽳㹳㰽⁡㵯≲≥㹦⁩⁥‼⁡㱁⽐㸠㱹‣⼱㸲㰳㴠≴∠㸻†⁸†‽䄠※⬠⁹ㄠ†㵸†㰠⼣㸼㰯⁡⽮㸾㰼㴯∾≮㸠⁣‽㱮⽥㹰㱡⁣⽬㹡㱳⁥㴠∼∠㹣⁳㱹⽷㸾㱥⁩⽦㸼㰯⽳㸾഼ਯ⁳⁰⁡‾‼ ⁳⁰⁡‾㰼⽢㸯ാ਼ ⁰⁲⁥‾‍ †㰠⼠㸠ഠਠ††††㰼⼯㸠ഠਠ㰠⼠㹴൲ਾ㰍⼊㸠ഠਠ㰼㹴‾‼⁢‾″⁥䥲⁥㽴‼″⁰⁢⁴⁥⁩⁧⁥Ⱪ⁲††⁥⁩⁴⁥⹴㰬⼠㹸ൣ੨㱡㍧⁥㵨≥ⴠ≯㹶⁴⡩⥡㱮⽤㍮㹯ഠੰ㱡㹲⁲⁤⁴⁡†⁴⁙⁩†⁴⁥⁨⁧⁥⁥Ɱ†⁴⁨ⱦ†‮⁣⁩⁣⹥⁳䉴Ⱐ⁤⁥†⁳†⁡†⁴⹭⁵䥳†⁣⁳⁤⁲⁴‮‾‼†⁳⁣⁴⁣䍯Ɑ⁰⁩†⁳⁦⁳⁥⹩㱲⽥㸠൮ਠ㱰㹩䙲Ⱐ⁢⁸⁴⁳⁤†⁡⁴⁨⁡㨭†⠲⥰⁡⁲†⁲⁥⁃⁲⁩⁇⁷㭩⡤⥦††‸⁥⁳⁰†ⱶ⁥⁡⡺⥨⁩⁲⁥⁥⹥⁳⁥⁩⁴⁳⹮㱴⽥㹥൲ੳ㰠㹲Ɐ†‮†⁣⁵†⁣⁨†⁲⁩⁹ⱱ⁵⁳⁶⁴⁨⸠⁡⁵†⁡⁦‬⁩⁡⁨⁳⹧㰠⽴㸠റਰ㰱㸠Ⱐ‵‰⁲⁦⁥⁳⁧⁧ ‾⁢Ⱐ†⁹ †‼⸠††Ɽ†⁡Ⱳ•††⁥⁳⁣⁡⸼㰯⽳㹡൮ਾ㰼㹲‼†⁳′ ⁢‽ ‾ †⁡⁩‴Ⱟ‾⁰•⸼ ‾⁰⁳ⱥ•‼‾‾⁡⁣⁳‾ ⸠⁣䥬Ⱪ‸⁡⁲†⁡‹⁡⁲‾†⁡•‾⁲‾⁡ⱡ⁳‼⁥††⁧⁧⁥⸠⁦†⁴⁡⁣⁡㽥††⁴⁲⁳⡧⁡⁨⁴⥩⁳⁩†‮‍⹉⁴䐠⁥†⁡†Ɽ†⁶†⁳⁩⁴⠠⁩⁳⥦⁴⁧†⁥⁴⁥⁧⁨⹤㰠⁲⽧㹥ഠੴ㱨㵯≲†≴㹨㱥⼠㹩൯੮㰠㹥൮੣⁵⁴㱥㹩൳ੴ†⁦†⁡†⁳⁩㱮㹥ഠ੥⁥⁥⁴‬†⁷⁨⁩⁣㱨⁹㵮≩≳㸠൳੯⁲⁴⁥⁤‮‼ ⁰‾‍ ‼㱰㹡㱴⁴㵥≣≮㸠ㅴ㱩⽭㸠㱴⁳⼠㹡㱬⁴㵥≴≵㹯㈻㱳⼠㹭㱥⁨⽡㹴㰠⁡㵮≳≬㹥㍮㱧⽴㹯㱮⁡⽰㹥㱳㵗≥≧㹥㐠㱴⽨㹯㱮†⽬㹩㱳⁡㴠≮≡㹶㕥㰠⼨㹯㱲
⼠㹲㱵⁦㵴≨≗㹥㘠㱰⽲㹳㱥⼠㹥㱬⁴㵩≮≩㹳㜠㱰⽡㹗㱥⽷㸠㱮⁴㴬∠≭㹡㡮㱹⼠㹳㰠⼠㹷㱥⁢㵡≲∠㹷㥩㱴⽨㹮㱴†⽴㹨㱥⁵㵧≥∠㹧ㅲづ㱡⽴㹨㱡⼿㸠㱔㵩≳∠㹮ㄮㄠ㱔⽨㹲㱳⁣⽡㹳㱥⁣㴠≴≯㹦ㄠ㉯㱵⽲㹯㱲⁨⽭㸠㱩㵮∩∠㸯ㄾ㌍㰊⼼㹲㱥⽡㹳㱳⁧㵴∠≯㹮ㄢ㐾㰼⼯㹲㱥 ⼼㹴㱡‍㴼≴∾㸍ㄊ㔠㰠⼠㸠㰼‾⼍㸊㰠†㴠∼≬㹡ㅳ㙳㰽⼢㹥㱲‍⼊㸠㰠⼠㸠ഠਠ†††‼⁰⁲⁥‾‼⁳⁰⁡㱮⼠㹡൳ੳ‽•⁩⁥•‾‱‼ ⁳㱰‾㴾∼∠㹣൬੡⁳⁳‽•⁩⁥•‾′‼㰯㹮㰾 㵮∠≳㸽㰢•㵰≡∼㹳㱰㵬≩≳㹰㰼⽢㸼⁳㱰㵬≩∼㸯㰯⼾㹮㰠⁳㵥∢≡㹮⠾⥢㱲⼠㹰㩡㱮⼠㹳㰽⼢㸢㰾 ⽳㹰㱡⁲㵰≡≡㹳⁳‽㰢•㵰≡∾㸼㱡⽮㹳⁳⁩⡮⤢‾㬯⁳㱰⁢㵳≰≳㹳〽㰢⽬㸾㨱㰰⼼㹮㰾⁲⼠㸯㰾㵳∽≥㸢‾‱‱‼‾㴼⁢㰯⁡㵳≳∢㸾〱㰲⼼㹮㰼⽢㸼㱳⼠㹣㱬•㴾∱≰㹡‾‼⁢‼㵳⁰㰠⽣㸽㰢⽥㸢㰾⁳㵢≲≳㹰⁡†⁣⁳㴽•㱮⽥㸼㰯⁡⽮㸾㰼‾㴠≣∽㸢⁩⁥ 㵳⁰㰾⼼㸾㰼⁡⽮㸠㱣‽㴢∾≳㹰⁡‾‼㱢‼㵣≬≮㹥㰸⼼㹮‾⁢㱲⁳㵬≡≥㸢㰹⼼㹮‾㩢㱲⼠㹰㱡⁣⽬㹡㱳⁩㴰∼≮㸾‼⁢⁲† ‾㰼†㴽∢∱㸼㱰⽡㹲†‾㴼㵳⁰㩡㱳⽳㹮㱥′⼲㸼㰯‾㴾∼∠㹣⁡⁳⁳‽•⁩⸾⡡⤾㰼⽢㸼㱳⼠㹣㱬•㴾∲≰㹡‾‼⁢⁲†㰯⁡㵳≳∾㸲㱰⽡㹲†‾㭰⁡㩳㱳⼽㹥㰢‶⼼㸯㱳‼㴼≳≣㹬⁡⁳⁳‽•⁩⸷⠾⥢㱲⼠㹰㱡⁣⽬㹡㱳⁩㴸∼≮㸾‼⁢⁲† ‾㰼†㴽∢∹㸼㱮⼾㸯㨾㰼⽳㹣㱬⁳⼽㸢㱬‾㵰≡≲㸠 ‾‼ ⁰⁲⁥‾⸠⠠⤼㰯⽴㸠㰠†⼠㸠㰠‼㵡≳≯㹤⁥•‾‍㰊†㴠∠≰㹡㱳⼽㹥•⡡⥦⁵⭮⁣⬢‾⡳⥹㱷⽯㹤㱥 ⽳㹰㱡⁳㵬≡≴㹩⁴㱥⁲㵴∼≳㹰㱬⽡㹰㩡㱲⽡㸨㱩⽳㸩㰼㵰≡≳㹰⁡‾‼㱢‼㵣≬≮㹥㱰⽡㹡⁳㰢⽫㹲㱤⁩⽦㸼㰯⽳㸾ഠ੬⁥†
⁩⁴⁥⁳
†⁧㱴⼻㹳൰੡†⁣⁡⁳⁳‽㰢⽮㹢൥ੲ•‾‱‼㰯⽳㸼യੳ㱰⽡㸠യਾ㰼⽳㹡൮ਠ㱣㍡⁳㴢≬䵩ⴠ≩㹤䵤†㱥⽮㍩㹴൥੭㱳㸠䴯⁡⁡‽ ‾†•‾†⁴⁤⹡䤾 ⁡⵲⁡‽⁥†⁦⁩†⁥⁡ⱳ‽⁢•⁳⁩⁝Ᵽ⁥⁌⹡㱬⽦㸯൳ੰ㱡㸾⁲‼†‾䴠⁩㩩⁳⁥⁤⁝⠠⁡⁳⤢‾⁩⁦⹰⁡⁢‾⁣••†
†⁳䩰⁣⁳Ɱ⁴†⁲⁨⁴‼⁰‼⁲⹮㰠⽣㹡൳ੳ㰽㹬䅩†⁥⡓ⱴ
⁧⁩⁳⁳⁨Ⱨ⁨⁳⁴⥡ ⁡⁲⹡ⱡ⁳‾†⁳⁣‣⁨㩴⁥⁳⠼⥳⁰‼⁢‾†⁡ㅩ‾†ⱳ⁰†⡣⥳⁳⁵•‼⁡⁳⁳㉳‽⁵⁲Ɱ‾⁰‾‼†‾†‽⁷⁤⁳‼㍳⹰⁡䵮⁡⁲‾ ⁲⁥⠨ ⤼⁢‼㍳⹰⁡⁳†⁳•⁥ㅮ⁴‣⁨㈠†⁴⁡⁳‼‾⁡‽⁩‾†‼†㍣⁳⁥⸢‾⁳†⁴㼠‼⁡⁡⡥⥷‾⁳†⁲⁳Ɫ⁲‼⡳⥮†⁳††⁳‽‣⁨⁥⁳‾⁡‼⁳⁣•ㅬ⁩‾㈠⸠††⁣⁲‾⁡‾⹦⁴⁳⁴⁧⁲⁰‾ ⁡†ㄠ††⁴㉥Ɑ⁳⁝⁦⁛‼‼ ⁡⁥†㌠⸠㰠⼠㸠ഠ੬㰠㸽⁳†⁳㍢⁥‱⁡‾ ⁳⁳†⁴⁔ㅨⱥ†⁴†⁥⁳†⁩•†⁰⁣⁳⁥㉹⹷• ⰾ›⁲⁳ㄽ•⁥㈢ⰾ††⁴㍥⁩⹴㱌⽩㹴൛ੲ㱝㸯⁰⁡‾‼⁢⁲㰠㸼䕳††††‼⁳••⁳‾‾ⸯ㰾⼼㹡൮ਠ⁣⁡⁳⁳‽•㱩㸢⸼⁰‽⡭⥥‣⁥⁨⁹‼⁰‾⁰⁣⸢㰾⼠㸠ഠਠ‼⁳⁰⁡㱮⼠㹡൳ੳ‽•⁥㱹⽷㹤ഢਾ⁥⁩⁦㰼㹰†⁴⁴‼⁡ Ɱ†⁳•⁥†††⁩⹦㱴⽌㹴൛੬⁝‼ ⁳㱰㸾䄼⁢⁳⁣‽‾䔠䄠䐠䴠䔠†‽⁡•⁥⁰⹳㱰⽡㸼ൢੲ† ‾‼㱳㹮⁡††⁡‽⁏⁴†⁨⹥㱬⽥㹮൴ੳ‼ ⁳⁰㱡㸼‾‾⁳⁩†㉳⸽㰢⽫㹷൯ੲ⁤•‾⁥㱬㸼⁡ ⁢‾⁡‾†††⁨⹳㱴⽛㸼യੳ㱰⽡㸼br /> r += 1

Your Task

You task is to:

  • Implement a testing harness for sorting algorithms. You will do this using getopt.
  • Implement five specified sorting algorithms.
  • Gather statistics about their performance.

Specifics

You must use getopt to parse the command line arguments. To get you started, here is a hint.

1
while ((c = getopt( argc, argv, " AmbiqMp : r : n :" )) != -1)

  • -A means employ all sorting algorithms.
  • -m means enable minSort.
  • -b means enable bubbleSort.
  • -i means enable insertionSort.
  • -q means enable quickSort.
  • -M means enable mergeSort.
  • -p n means print the first n elements of the array. The default value is 100.
  • -r s means set the random seed to s. The default is 8222022.
  • -n c means set the array size to c. The default value is 100.

It is important to read this carefully. None of these options is exclusive of any other (you may specify any number of them, including zero).

  • Your random numbers should be 24 bits, no larger(2^24 - 1 = 16777215).
  • You must use rand() and srand(), not because they are good (they are not), but because they are what is specified by the C99 standard.
  • Your program must be able to sort any number of random integers up to the memory limit of the computer. That means that you will need to dynamically allocate the array using calloc().
  • Your program should have no memory leaks.
    A large part of this assignment is understanding and comparing the performance of various sorting algorithms. Consequently, you must collect some simple statistics on each algorithm. In particular,
  • The size of the array,
  • The number of moves required (each time you transfer an element in the array, that counts), and
  • The number of comparisons required (comparisons only count for elements, not for logic).

Submission

You must turn in your assignment in the following manner:

  1. Have file called Makefile that when the grader types make will compile your program. At this point you will have learned about make and can create your own Makefile.
    • CFLAGS=-Wall -Wextra -Werror -pedantic must be included.
    • CC=gcc must be specified.
    • make clean must remove all files that are compiler generated.
    • make should build your program, as should make all.
  2. You program must have the source and header files:
    • minsort.h specifies the interface to minSort().
    • minsort.c implements minSort().
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!