Csharp/C Sharp/Collections Data Structure/Sort

Материал из .Net Framework эксперт
Версия от 11:39, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A simple version of the Quicksort

/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
// A simple version of the Quicksort. 
 
using System; 
 
class Quicksort { 
 
  // Set up a call to the actual Quicksort method. 
  public static void qsort(char[] items) { 
    qs(items, 0, items.Length-1); 
  } 
 
  // A recursive version of Quicksort for characters. 
  static void qs(char[] items, int left, int right)  
  {  
    int i, j;  
    char x, y;  
  
    i = left; j = right;  
    x = items[(left+right)/2];  
  
    do {  
      while((items[i] < x) && (i < right)) i++;  
      while((x < items[j]) && (j > left)) j--;  
  
      if(i <= j) {  
        y = items[i];  
        items[i] = items[j];  
        items[j] = y;  
        i++; j--;  
      }  
    } while(i <= j);  
  
    if(left < j) qs(items, left, j);  
    if(i < right) qs(items, i, right);  
  } 
} 
 
public class QSDemo { 
  public static void Main() { 
    char[] a = { "d", "x", "a", "r", "p", "j", "i" }; 
    int i; 
 
    Console.Write("Original array: "); 
    for(i=0; i < a.Length; i++)  
      Console.Write(a[i]); 
 
    Console.WriteLine(); 
 
    // now, sort the array 
    Quicksort.qsort(a); 
     
    Console.Write("Sorted array: "); 
    for(i=0; i < a.Length; i++)  
      Console.Write(a[i]); 
  } 
}


Bubble sort

using System;

  delegate bool Comparator(object lhs, object rhs);
  class MainEntryPoint {
    static void Main() {
      Employee [] employees = {
              new Employee("A", 20000),
              new Employee("B", 10000),
              new Employee("C", 25000),
              new Employee("D", 99999),
              new Employee("E", 23000),
              new Employee("F", 50000)};
              
      BubbleSorter.Sort(employees, new Comparator(Employee.IsGreater));
      for (int i=0 ; i<employees.Length ; i++){
        Console.WriteLine(employees[i].ToString());
      }  
    }
  }
  class Employee {
    private string name;
    private decimal salary;
    public Employee(string name, decimal salary)
    {
      this.name = name;
      this.salary = salary;
    }
    public override string ToString()
    {
      return string.Format(name + ", {0:C}", salary);
    }
    public static bool IsGreater(object a, object b)
    {
      Employee empA = (Employee) a;
      Employee empB = (Employee) b;
      return (empA.salary > empB.salary) ? true : false;
    }
  }
  class BubbleSorter
  {
    static public void Sort(object [] sortArray, Comparator gtMethod) {
      for (int i=0 ; i<sortArray.Length ; i++) {
        for (int j=i+1 ; j<sortArray.Length ; j++) {
          if (gtMethod(sortArray[j], sortArray[i])) {
            object temp = sortArray[i];
            sortArray[i] = sortArray[j];
            sortArray[j] = temp;
          }
        }
      }
    }
  }


Demonstrate the Bubble sort

/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/* 
   Project 5-1 
   Demonstrate the Bubble sort. 
*/ 
using System; 
 
public class BubbleSort {  
  public static void Main() {  
    int[] nums = { 99, -10, 100123, 18, -978, 
                   5623, 463, -9, 287, 49 }; 
    int a, b, t;  
    int size;  
  
    size = 10; // number of elements to sort  
  
    // display original array  
    Console.Write("Original array is:"); 
    for(int i=0; i < size; i++) 
      Console.Write(" " + nums[i]);  
    Console.WriteLine();  
  
    // This is the bubble sort.  
    for(a=1; a < size; a++)  
      for(b=size-1; b >= a; b--) {  
        if(nums[b-1] > nums[b]) { // if out of order  
          // exchange elements   
          t = nums[b-1];  
          nums[b-1] = nums[b];  
          nums[b] = t;  
        }  
      }  
  
    // display sorted array  
    Console.Write("Sorted array is:");  
    for(int i=0; i < size; i++) 
      Console.Write(" " + nums[i]);  
    Console.WriteLine(); 
  } 
}


Implements the recursive merge sort algorithm to sort an array

using System;
public class MergeSort {
  public static void Sort (int[] data, int left, int right) {
    if (left < right) {
      int middle = (left + right)/2;
      Sort(data, left, middle);
      Sort(data, middle + 1, right);
      Merge(data,left, middle, middle+1, right);
    }
  }
  public static void Merge(int[] data, int left, int middle, int middle1, int right) {
    int oldPosition = left;
    int size = right - left + 1;
    int[] temp = new int[size];
    int i = 0;  
    
    while (left <= middle && middle1 <= right) {
      if (data[left] <= data[middle1]) 
        temp[i++] = data[left++];
      else
        temp[i++] = data[middle1++];
    }
    if (left > middle) 
      for (int j = middle1; j <= right; j++)
        temp[i++] = data[middle1++];
    else
      for (int j = left; j <= middle; j++)
        temp[i++] = data[left++];
    Array.Copy(temp, 0, data, oldPosition, size);
  }
  
  public static void Main (String[] args) {
    int[] data = new int[]{2,3,1,6,2,98,4,6,4,3,45};
    
    for (int i = 0; i < data.Length; i++) { 
      Console.WriteLine(data[i]);
    }
    Sort(data, 0, data.Length-1);
    for (int i = 0; i < data.Length; i++) { 
      Console.WriteLine(data[i]);
    }
  }
}


Sorts an array of data using the insertion sort algorithm

using System;
public class InsertionSort {
    
  public static void InsertNext(int i, int[] item) {
    int current = item[i];
    int j = 0;
    while (current > item[j]) j++;
    for (int k = i; k > j; k--)
      item[k] = item[k-1];
    item[j] = current;
  }
  public static void Sort(int[] item) {
    for (int i = 1; i < item.Length; i++) {
      InsertNext(i, item); 
    }
  }
  public static void Main()  {
    int[] item = new int[]{2,4,1,6,3,8,1,0,2,6,3,6};
    Sort(item);
    for(int i=0; i<item.Length;i++){
        Console.WriteLine(item[i]);
    }
  }
}