Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:12:03+00:00 2026-05-25T22:12:03+00:00

class Sort { int[] arr; int counter=0; //Constructor public Sort() { arr = new

  • 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();

    }

I implement my Sort class for sorting integers and place int counter to determine number of swaps and comparsions. But I am not sure it is working correctly! Is there any other way to determine number of swaping and comparsions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:12:04+00:00Added an answer on May 25, 2026 at 10:12 pm

    You could create a class which implements IComparable to count the comparator access and a specialized collection which counts the swaps. Like that you dont have to count the access inside of the sort algorithms and you delegate the tasks more strict to code parts.

    In the index operator you count the swap operations and in the IComparable implementation you count the comparisons

    Example for a class SortItem which implements the IComparable:

    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
    }
    

    and the sorting collection:

    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;
        }
    
    
    }
    

    Here the execution:

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package hw3; public class Main { public static void main(String[] args) { final int
//node.h class node { public: void sort(node n); }; I didn't try the code
Given: case class Foo(a: Int, b: String, c: Double) you can say: val params
I have an example class containing two data points: public enum Sort { First,
Say you have this: class LogEntry { int ID; int UserName; datetime TimeStamp; string
If I have in C++: class A { private: virtual int myfunction(void) {return 1;}
#include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts
I have a list class that has a Title (string) Rank (int) and a
I've got this trivial class hierarchy: class Base { public: virtual int x( )
Take a look at the following program: class Test { List<int> myList = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.