10 Kasım 2013 Pazar

Some tiny codes in C# ( Form Application )

    I know it's been a long time , but i'm back.Today i am going to put some codes in C# , i don't like it though.Do not expect so much  things from these codes because these two coding were little homework in my Data Structures class so they simply sort the numbers that you put on the text field, and one button to run these codes and ofcourse the other text field to show the ordered-numbers.I used "selectionsort" and "bubble sort" algorithms.I hope , beginners of C# can get inpiration by looking at these codes.

Insertion Sort C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sorting.examples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            string text = textBox1.Text;
            string[] words = text.Split(delimiterChars);
            int[] intValues = new int[words.Length];

            for (int i = 0; i < words.Length; i++)
            {
                intValues[i] = Convert.ToInt32(words[i]);
            }
            int j,temp;
            
            for ( j = 1; j < intValues.Length; j++) {

                for (int i = 0; i < intValues.Length-j; i++)
                {
              if (intValues[i] > intValues[i + 1])
                    {
                        temp = intValues[i + 1];
                        intValues[i + 1] = intValues[i];
                        intValues[i] = temp;
                    }
                }
            }
            var result = string.Join(",", intValues);
            textBox2.Text = result;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {  
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
        } 
    }
}
Bubble Sort C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sorting.examples
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            string text = textBox1.Text;
            string[] words = text.Split(delimiterChars);
            int[] intValues = new int[words.Length];

            for (int i = 0; i < words.Length; i++)
            {

                intValues[i] = Convert.ToInt32(words[i]);

            }

            int j,temp;
            
            for ( j = 1; j < intValues.Length; j++) {

                for (int i = 0; i < intValues.Length-j; i++)
                {


                    if (intValues[i] > intValues[i + 1])
                    {
                        temp = intValues[i + 1];
                        intValues[i + 1] = intValues[i];
                        intValues[i] = temp;
                    }
                }
            }

            var result = string.Join(",", intValues);
            textBox2.Text = result;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

       
    }
}