Advertisement
SuperLemrick

Insertion Sort

Dec 20th, 2014
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. /* TeenCoder: Windows Programming
  11.    
  12.    ToDo List Application
  13.  
  14.    Copyright 2013 CompuScholar, Inc.
  15. */
  16.  
  17. namespace Insertion_Sort
  18. {
  19.     public partial class SortForm : Form
  20.     {
  21.         // Create and initialize the linked list of integers
  22.         LinkedList<int> numericList = new LinkedList<int>();
  23.  
  24.         public SortForm()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         // This code is provided for the student in the Activity Starter
  30.         private void displayList()
  31.         {
  32.             // Clear the listbox
  33.             NumberList.Items.Clear();
  34.  
  35.             // Iterate over each item in the list
  36.             foreach (int item in numericList)
  37.             {
  38.                 // Add the item to the text box
  39.                 NumberList.Items.Add(item);
  40.             }
  41.         }
  42.  
  43.         // This code is provided for the student in the Activity Starter
  44.         private void AddFrontButton_Click(object sender, EventArgs e)
  45.         {
  46.             // Get the text from the item text box
  47.             int newItem = (int)UpDownList.Value;
  48.  
  49.             // Add to the front of the list
  50.             numericList.AddFirst(newItem);
  51.  
  52.             // Display the new list
  53.             displayList();
  54.         }
  55.  
  56.         // This code is provided for the student in the Activity Starter
  57.         private void ClearButton_Click(object sender, EventArgs e)
  58.         {
  59.             // Remove all items from the list
  60.             numericList.Clear();
  61.  
  62.             // Display the new list
  63.             displayList();
  64.         }
  65.  
  66.         // This code is provided for the student in the Activity Starter
  67.         private void RemoveButton_Click(object sender, EventArgs e)
  68.         {
  69.               if ((NumberList.Text == null) || ( NumberList.Text.Length == 0))
  70.                 return;
  71.                
  72.             // Get the text from the item text box
  73.             int removeItem = int.Parse(NumberList.Text);
  74.  
  75.             // Iterate over the list looking for a matching string
  76.             LinkedListNode<int> node = numericList.First;
  77.             while (node != null)
  78.             {
  79.                 if (node.Value == removeItem)
  80.                 {
  81.                     // Remove this node from the list
  82.                     numericList.Remove(node);
  83.                     break;  // end of search
  84.                 }
  85.                
  86.                 // Move to the next entry in the list
  87.                 node = node.Next;  
  88.             }
  89.  
  90.             // Display the new list
  91.             displayList();
  92.         }
  93.  
  94.         // This code is ADDED FOR ACTIVITY by the student
  95.         private void SortButton_Click(object sender, EventArgs e)
  96.         {
  97.             if (numericList.Count < 2)
  98.                 return;
  99.  
  100.             LinkedListNode<int> currentNode = numericList.First.Next;
  101.  
  102.             while (currentNode != null)
  103.             {
  104.                 LinkedListNode<int> nextNode = currentNode.Next;
  105.  
  106.                 numericList.Remove(currentNode);
  107.  
  108.                 LinkedListNode<int> searchNode = numericList.First;
  109.                 while ((searchNode != null) && (searchNode.Value < currentNode.Value))
  110.                 {
  111.                     searchNode = searchNode.Next;
  112.                 }
  113.                 if (searchNode != null)
  114.                     numericList.AddBefore(searchNode, currentNode.Value);
  115.                 else
  116.                     numericList.AddLast(currentNode);
  117.  
  118.                 currentNode = nextNode;
  119.             }
  120.             displayList();
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement