2

I trying to do sorting without use of any method or function

My Code :

   string[] names = { "Flag", "Nest", "Cup", "Burg", "Yatch", "Next" };                     
   string name = string.Empty;
   Console.WriteLine("Sorted Strings : ");

   for (int i = 0; i < names.Length; i++)
        {
            for (int j = i + 1; j < names.Length; j++)
            {
                for (int c = 0; c < names.Length; c++)
                {
                    if (names[i][c] > names[j][c])
                    {
                        name = names[i];
                        names[i] = names[j];
                        names[j] = name;
                    }
                }

            }
            Console.WriteLine(names[i]);
        }

Please let me bring any solution for this code ?

In this code i am getting "Index was outside the bounds of the array" exception

7
  • 1
    And why don't you want to use a method or function? It could be as easy as using a simple LINQ statement... Commented May 20, 2014 at 11:30
  • If you do not want to execute any piece of code (resulting in a call of a method or function) there is not much left available that can solve this. Commented May 20, 2014 at 11:30
  • What specifically are your problems with this code? You get better answers if you ask specific questions. See also stackoverflow.com/help/how-to-ask Commented May 20, 2014 at 12:13
  • You don't need three loops over that names array. Two is enough. Or should that c loop go over names[i].Length? Commented May 20, 2014 at 12:16
  • @ReinderWit i did with use of method & function... i am try without that.... so oly like this... Commented May 20, 2014 at 12:35

11 Answers 11

5
        int temp = 0;
        int[] arr = new int[] { 20, 65, 98, 71, 64, 11, 2, 80, 5, 6, 100, 50, 13, 9, 80, 454 };
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = i + 1; j < arr.Length; j++)
            {
                if (arr[i] > arr[j])
                {
                    temp = arr[j];
                    arr[j] = arr[i];
                    arr[i] = temp;
                }
            }
            Console.WriteLine(arr[i]);
        }
        Console.ReadKey();
Sign up to request clarification or add additional context in comments.

Comments

3

You need to implement a sorting algorithm.

A very simple algorithm you can implement is the insertion sort:

string[] names = { "Flag", "Nest", "Cup", "Burg", "Yatch", "Next" };

for (int i = 0; i < names.Length; i++)
{
    var x = names[i];
    var j = i;
    while(j > 0 && names[j-1].CompareTo(x) > 0)
    {
        names[j] = names[j-1];
        j = j-1;
    }
    names[j] = x;
}

5 Comments

@siva What do you mean, without algorithms? You're asking for an algorithm!
i am not saying like that... am trying with swapping & looping
@siva That's exactly what this answer does
Is this question about helping OP by telling what he did wrong, or is it about dumping your favorite sort implementation?
ya here used string CompareTo method. my expected result is without any use of method is this possible to do sorting?
2
string[] names = { "Flag", "Next", "Cup", "Burg", "Yatch", "Nest" };
        string name = string.Empty;
        Console.WriteLine("Sorted Strings : ");

        for (int i = 0; i < names.Length; i++)
        {
            int c = 0;
            for (int j = 1; j < names.Length; j++)
            {
                if (j > i)
                {
                 Sort:
                    if (names[i][c] != names[j][c])
                    {
                        if (names[i][c] > names[j][c])
                        {
                            name = names[i];
                            names[i] = names[j];
                            names[j] = name;
                        }
                    }
                    else
                    {
                        c = c + 1;
                        goto Sort;
                    }                        
                }
            }
            Console.WriteLine(names[i]);
        }

Comments

1

I you were conflicting in length of names array and comparing string. Below is the working solution . I have tested it it's working now

static void Main(string[] args)
        {
            int min=0;

            string[] names = { "Flag", "Nest", "Cup", "Burg", "Yatch", "Next" };
            string name = string.Empty;
            Console.WriteLine("Sorted Strings : ");

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

                    if(names[i].Length < names[j].Length)
                           min =names[i].Length;
                    else
                            min =names[j].Length;
                    for(int k=0; k<min;k++)
                    {
                        if (names[i][k] > names[j][k])
                        {
                            name = names[i].ToString();
                            names[i] = names[j];
                            names[j] = name;
                            break;
                        }
                        else if(names[i][k] == names[j][k])
                        {
                            continue;
                        }
                        else
                        {
                            break;
                        }

                    }
                }


            }
            for(int i= 0;i<names.Length;i++)
            {
                Console.WriteLine(names[i]);
                Console.ReadLine();
            }
        }
    }

Comments

0
class Program
{
    static void Main(string[] args)
    {
        int[] arr = new int[] {9,1,6,3,7,2,4};
        int temp = 0;

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

            Console.Write(arr[i]+",");
        }
             Console.ReadLine();
    }

1 Comment

You should explain to him why you did what. Otherwise it is just a piece of code.
0
for (int i = 0; i < names.Length; i++)
{
   string temp = "";
   for (int j = i + 1; j < names.Length; j++)
   {
      if (names[i].CompareTo(names[j]) > 0)
      {
           temp = names[j];
           names[j] = names[i];
           names[i] = temp;
      }
    }

}

1 Comment

string[] names = { "Flag", "Fig", "Nest", "Cup", "Burg", "Yatch", "Next" };
0
public int compareing(string a, string b)
        {


            char[] one = a.ToLower().ToCharArray();
            char[] two = b.ToLower().ToCharArray();

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

                for (int j = 0; j < two.Length; j++)
                {
                    Loop:
                    int val = 0;
                    int val2 = 0;

                    string c = one[i].ToString();
                    char[] c1 = c.ToCharArray();
                    byte[] b1 = ASCIIEncoding.ASCII.GetBytes(c1);

                    string A = two[j].ToString();
                    char[] a1 = A.ToCharArray();
                    byte[] d1 = ASCIIEncoding.ASCII.GetBytes(a1);


                    int sec = d1[0];
                    int fir = b1[0];


                    if (fir > sec)
                    {
                        return ret = 1;
                        break;
                    }
                    else
                    {
                        if (fir == sec)
                        {
                            j = j + 1;
                            i = i + 1;
                            if (one.Length == i)
                            {
                                return ret = 0;
                            }
                            goto Loop;
                        }
                        else
                        {
                            return 0;
                        }

                    }




                }


            }

            return ret;
        }
        public void stringcomparision(List<string> li)
        {
            string temp = "";
            for(int i=0;i<li.Count;i++)
            {
                for(int j=i+1;j<li.Count;j++)
                {

                    if(compareing(li[i],li[j])>0)
                    {
                        //if grater than it throw 1 else -1
                        temp = li[j];
                        li[j] = li[i];
                        li[i] = temp;
                    }
                }
            }


            Console.WriteLine(li);
        }

1 Comment

convert the string char array. same way to done second char also. iterate the both characters. check the each character ASCII code and compere.
0
for (int i = 0; i < names.Length - 1; i++)
        {
            string temp = string.Empty;
            for (int j = i + 1; j < names.Length; j++)
            {
                if (names[i][0] > names[j][0])
                {
                    temp = names[i].ToString();
                    names[i] = names[j].ToString();
                    names[j] = temp;
                }
            }
        }
        for (int i = 0; i < names.Length - 1; i++)
        {
            int l = 0;
            if (names[i][0] == names[i + 1][0])
            {
                string temp = string.Empty;
                if (names[i].Length > names[i + 1].Length)
                    l = names[i + 1].Length;
                else
                    l = names[i].Length;
                for (int j = 0; j < l; j++)
                {
                    if (names[i][j] != names[i + 1][j])
                    {
                        if (names[i][j] > names[i + 1][j])
                        {
                            temp = names[i].ToString();
                            names[i] = names[i + 1].ToString();
                            names[i + 1] = temp;
                        }
                        break;
                    }
                }

            }
        }
        foreach (var item in names)
        {
            Console.WriteLine(item.ToString());
        }

2 Comments

Please add an explanation to your solution
First sort it based on all first letter of array string then sort it based on consecutive letter of array string
0
            string[] names = { "Flag", "Nest", "Cup", "Burg", "Yatch", "Next" };
            string temp = "";
            int tempX = 0, tempY = 0;
            int tempX1 = 0, tempY1 = 0;
            for (int i = 0; i<names.Length; i++)
            {
                for (int j = i+1; j<names.Length; j++)
                {

                    if (((string)names[i])[0] > ((string)names[j])[0])
                    {
                        temp=(string)names[i];
                        names[i]=names[j];
                        names[j]=temp;
                    }
                    else if (((string)names[i])[0] == ((string)names[j])[0])
                    {
                        tempX=0; tempY=0;
                        tempX1=names[i].Length;
                        tempY1=names[j].Length;
                        while (tempX1 > 0 && tempY1 >0)
                        {
                            if (((string)names[i])[tempX] !=((string)names[j])[tempY])
                            {
                                if (((string)names[i])[tempX]>((string)names[j])[tempY])
                                {
                                    temp=(string)names[i];
                                    names[i]=names[j];
                                    names[j]=temp;

                                    break;
                                }
                            }
                            tempX++;
                            tempY++;
                            tempX1--;
                            tempY1--;
                        }

                    }
                }
            }

4 Comments

Hi JasrajKumawat, welcome. Please consider adding an explanation and format properly your answer.
Above asked Question was Ambiguous, which is has been Corrected now. Previously user was not comparing conditions correctly. But now code is perfectly tested and working fine. Thank you.
What is the code doing? Adding an explanation to that question would indeed increase your answer's value.
my code is comparing ASCII values, and it first checks value at '0' index of array, and above asked question compares all characters so the code did not work properly.
0

You can do it using bubble sort:

Assume you have the array of names called name

The tempName is just to not change the original array (You can use the original array instead)

void sortStudentsAlphabetically()
{
     int nameIndex;
     string temp;
     string[] tempName = name;
     bool swapped = true;
    
     for(int i = 0; i < name.Length-1 && swapped ; i++)
     {
         swapped = false;

         for(int j = 0; j < name.Length-1; j++)
         {
             nameIndex = 0;
             recheck:
                 if (name[j][nameIndex]> name[j+1][nameIndex]) 
                 {
                     temp = tempName[j];
                     tempName[j] = tempName[j+1];
                     tempName[j+1] = temp;
                     swapped = true;
                 }
    
                 if (name[j][nameIndex] == name[j + 1][nameIndex])
                 {
                     nameIndex++;
                     goto recheck;
                 }
          }
   }
    
   foreach(string x in tempName)
   {
         Console.WriteLine(x);
   }
}

Comments

0

User Below code :

int[] arrayList = new int[] {2,9,4,3,5,1,7};  
int temp = 0;  

for (int i = 0; i <= arrayList.Length-1; i++)  
{  
    for (int j = i+1; j < arrayList.Length; j++)  
    {  
        if (arrayList[i] > arrayList[j])  
        {  
            temp = arrayList[i];  
            arrayList[i] = arrayList[j];  
            arrayList[j] = temp;  
        }  
    }  
}  
Console.WriteLine("Sorting array in ascending order : ");  
foreach (var item in arrayList)  
{  
    Console.WriteLine(item);  
}  
Console.ReadLine();

Output:

Sorting array in ascending order : 1 2 3 4 5 7 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.