Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- //You are given an array of strings (one line, space-delimited). Sort the array using the Bubble sort or Insertion sort algorithms. //Instead of comparing if one string is smaller than the other (which you can’t do), you can use the string.CompareTo(str) method. Once //the array is sorted, print it on the console, separating the elements by spaces.
- //namespace Sort_array_of_strings
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] array = Console.ReadLine().Split(' ').ToArray();
- BubbleSort(array);
- Console.WriteLine(string.Join(" ", array));
- }
- static void BubbleSort(string[] array)
- {
- bool swapped;
- do
- {
- swapped = false;
- for (int i = 0; i < array.Length-1; i++)
- {
- if (array[i].CompareTo(array[i+1]) == 1)
- {
- Swap(array, i, i + 1);
- swapped = true;
- }
- }
- } while (swapped);
- }
- static void Swap(string[]array, int i1, int i2)
- {
- string temp = array[i1];
- array[i1] = array[i2];
- array[i2] = temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement