2011-10-03 4 views
0
class Sort 
{ 
    int[] arr; 
    int counter=0; 

    //Constructor 
    public Sort() 
    { 
     arr = new int[10000]; 
    } 

    string address; 

    public void SwitchCase(int Case) 
    { 
      switch (Case) 
      { 
       case 1: 
        address = @"C:\Users\Aqib Saeed\Desktop\case1.txt"; 
        break; 
       case 2: 
        address = @"C:\Users\Aqib Saeed\Desktop\case2.txt"; 
        break; 
       case 3: 
        address = @"C:\Users\Aqib Saeed\Desktop\case3.txt"; 
        break; 
       default: 
        break; 
      } 
    } 

    //Read file for input 
    public void FillArray() 
    { 
     using (StreamReader rdr = new StreamReader(address)) 
     { 
      for (int i = 0; i < arr.Length; i++) 
      { 
       arr[i] = Convert.ToInt32(rdr.ReadLine()); 
      } 
     } 
    } 


    // Insertion Sort 
    public void InsertionSort() 
    { 
     int insert; 

     for (int i = 1; i < arr.Length; i++) 
     { 
      insert = arr[i]; 

      int moveItem = i; 

      while (moveItem > 0 && arr[moveItem - 1] > insert) 
      { 
       arr[moveItem] = arr[moveItem - 1]; 
       moveItem--; 
       counter++; 
      } 
      arr[moveItem] = insert; 

     } 
    } 

    public void Counter() 
    { 
     Console.WriteLine(counter); 
    } 

    //Bubble Sorting 
    public void BubbleSort() 
    { 
     int temp; 

     for (int i = 0; i < arr.Length; i++) 
     { 
      for (int j = 0; j < arr.Length - 1 - i; j++) 
      { 
       if (arr[j] > arr[j + 1]) 
       { 
        temp = arr[j]; 
        arr[j] = arr[j + 1]; 
        arr[j + 1] = temp; 
        counter++; 
       } 
      } 
     } 
    } 

    //Selection Sorting 
    public void SelectionSort() 
    { 
     int min, temp; 

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

      for (int j = i + 1; j < arr.Length; j++) 
      if (arr[j] < arr[min]) 

      min = j; 

      temp = arr[i]; 
      arr[i] = arr[min]; 
      arr[min] = temp; 
      counter++; 
     } 
    } 

    // Write Output to file 
    public void Writer() 
    { 
     using (StreamWriter wrtr = new StreamWriter(@"C:\Users\AqibSaeed\Desktop\SortedOutput.txt")) 
     { 
      for (int i = 0; i < arr.Length; i++) 
      { 
       wrtr.WriteLine(arr[i]); 
      } 
     } 
    } 
} 

static void Main(string[] args) 
    { 
     Sort srt = new Sort(); 

     Console.WriteLine("Enter Case 1 OR 2 OR 3"); 


     srt.SwitchCase(Convert.ToInt32(Console.ReadLine())); 

     srt.FillArray(); 

     srt.BubbleSort(); 

     srt.Writer(); 

     Console.WriteLine("Sorted Output File Is Ready"); 
     srt.Counter(); 
     Console.ReadLine(); 

    } 

스와핑 및 comparsions 수를 결정하기 위해 정수 정렬 및 int 카운터 배치에 대한 내 Sort 클래스를 구현합니다. 하지만 제대로 작동하는지 확신 할 수 없습니다. 스왑 및 comparsions의 수를 결정하는 다른 방법이 있습니까?각 정렬 알고리즘에서 스와핑 및 컴마네이션은 몇 개입니까?

+0

무엇이 필요합니까? 왜 제대로 작동하지 않는다고 생각합니까? Btw 어디서나 비교 횟수를 세지는 못합니다. – Howard

+0

나는 그 권리를 확신하지 못했습니다! u는 내가 옳은 일을한다고 말해 줄 수 있습니까 –

답변

0

IComparable을 구현하여 비교기 액세스를 계산하는 클래스와 스왑을 계산하는 특수 컬렉션을 만들 수 있습니다. 마찬가지로 정렬 알고리즘 내부의 액세스를 계산할 필요가 없으며 코드 파트에 더 엄격한 작업을 위임해야합니다. 인덱스 연산자에서

당신은 스왑 작업을 세고하여 IComparable 구현에 당신은에서 IComparable 구현하는 클래스의 SortItem의 비교

예 수 :

public class SortItem<T> : IComparable<T> where T : IComparable 
{ 
    private static int _ComparisonCount = 0; 
    public static int ComparisonCount 
    { 
     private set 
     { 
      _ComparisonCount = value; 
     } 
     get 
     { 
      return _ComparisonCount; 
     } 
    } 

    public T Value 
    { 
     get; 
     set; 
    } 

    public static void ResetComparisonCount() 
    { 
     _ComparisonCount = 0; 
    } 

    #region IComparable<T> Members 

    public int CompareTo(T other) 
    { 
     ComparisonCount++; 
     return this.Value.CompareTo(other); 
    } 

    #endregion 
} 

및 정렬 모음 :

public class SortCollection<T> : IList<SortItem<T>> 
{ 
    public SortCollection(IList<T> sortList) 
    { 
     InnerList = sortList; 
    } 

    private IList<T> InnerList = null; 

    public T this[int key] 
    { 
     get 
     { 
      return InnerList[key]; 
     } 
     set 
     { 
      SwapCount++; 
      InnerList[key] = value; 
     } 
    } 

    private int _SwapCount = 0; 
    public int SwapCount 
    { 
     private set 
     { 
      _SwapCount = value; 
     } 
     get 
     { 
      return _SwapCount; 
     } 
    } 

    public void ResetSwapCount() 
    { 
     _SwapCount = 0; 
    } 


} 

여기 실행 :

List<Int32> baseList = new List<int>(new Int32 {6, 2, 7, 3, 1, 6, 7 }); 
SortCollection<Int32> sortList = new SortCollection<int>(baseList); 

//do the sorting.... 

Console.WriteLine("Swaps: " + sortList.SwapCount.ToString()); 
Console.WriteLine("Comparisons: " + SortItem<Int32>.ComparisonCount.ToString()); 
SortItem<Int32>.ResetComparisonCount(); 
sortList.ResetSwapCount(); 
+0

IComparable이 comparsions의 수를 결정하는 방법을 언급 해주시겠습니까? –

+0

아직도 아이디어를 얻을 수 없다, 나는 그것을 구현하지만 어떻게 내가 메인 메서드 또는 내 SORT 클래스에서 이것을 부르는가? –

+0

작동 했습니까? 나는 실제로 코드를 테스트하지 못했고, 당신을위한 실제 액세스 계산을 할 수있는 특별한 Object/collection을 가지고 있다고 생각했다. – fixagon

0

스왑 만 계산하고 비교는 계산하지 않습니다. 비교를 계산하려면 if 비교를 통과 할 때마다 증가하는 추가 카운터를 추가해야합니다.

관련 문제