티스토리 뷰

AT2


//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
댓글
Announcement
Recent Posts
Recent Comments
Total
Today
Yesterday
Link
TAG
more
«   2024/12   »
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
Search by month