티스토리 뷰
AT2
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | //requirements. //Portfolio Assessment Activity AT1.3 //Q.6 Create a Windows Forms Application that sorts a list of random number by using the Shell Sort //algorithm. The program must use an Array of 29 integers. After each iteration the program must //display the Array. //procedure shellSort(array[]) //gap = array length //WHILE gap > 0 //FOR i = 0 TO arraylength-1 // j = i; //temp = array[i]; //WHILE j >= gap AND array[j - gap] > temp //array[j] = array[j - gap]; //j = j - gap; //END WHILE //array[j] = temp; //END FOR //gap = gap/2; //END WHILE //End procedure //For this activity, zip up the project folder and then upload. --result using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace AT1._3 { public partial class Form : System.Windows.Forms.Form { //Define the length of array and name of array static int max = 29; int [] myArray = new int [max]; public Form() { InitializeComponent(); //Fill random interger number between 0 to 100 Random rand = new Random(); for ( int i = 0; i < max; i++) { myArray[i] = rand .Next(100); } //Show unsorted list of number ShowArrayList(); } private void btnsort_Click(object sender, EventArgs e) { //Define some veriables int i, j, temp,gap = myArray.Length; //When gap is below than 0, escape the loop while (gap>0) { for (i = 0; i < (myArray.Length); i++) { //keep the number of i to j j = i; //keep the data on myArray[i] to temp temp = myArray[i]; //j>=gap -- Run while when j(i) is bigger than gap, for example change i=14, gap=14 //myArray[j-gap]>temp -- Change the order when the first one is bigger. for example change myArray[0] and myArray[14] while ((j>=gap) && (myArray[j-gap]>temp)) { //Swap the data, Using temp //Swap A and B Using temp //temp=B, B=A, A=temp myArray[j] = myArray[j - gap]; j = j - gap; } myArray[j] = temp; ShowArrayList(); } //End of for //Devide gap by 2 if (gap / 2 != 0) gap = gap / 2; //When the gap is 1, make 0 and escape while roop else if (gap == 1) gap = 0; //Otherwise, make it 1 and run final roop else gap = 1; } //Show array function ShowArrayList(); } private void ShowArrayList() { //clear listbox first listarray.Items.Clear(); //Using for statement, show every array till 29th of array for ( int i = 0; i < max; i++) { listarray.Items.Add(myArray[i]); } } // End of ShowArrayList } } |
before / after
'AU Study > TAFE Assessment' 카테고리의 다른 글
C sharp programming AT 1.1 (0) | 2017.10.25 |
---|---|
C sharp programming AT 1.2 (0) | 2017.10.25 |
C sharp programming AT 1.4 (0) | 2017.10.25 |
Javascript Portfolio (0) | 2017.10.24 |
C sharp programming AT 2 (1) | 2017.10.22 |