Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using java.util;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Diagnostics;
- namespace SoftSvetlina
- {
- class Program
- {
- static void BubbleSort(List<int> list)
- {
- for (int j = 0; j < list.Count - 1; j++)
- {
- bool flag = false;
- for (int i = 0; i < list.Count - j - 1; i++)
- {
- if (SumDigits(list[i]) > SumDigits(list[i + 1]) ||
- (SumDigits(list[i]) == SumDigits(list[i + 1]) && list[i] > list[i + 1]))
- {
- flag = true;
- int temp = list[i];
- list[i] = list[i + 1];
- list[i + 1] = temp;
- }
- }
- if (!flag)
- {
- break;
- }
- }
- }
- static int SumDigits(int number)
- {
- int sum = 0;
- while (number != 0)
- {
- sum += number % 10;
- number /= 10;
- }
- return sum;
- }
- static void Main()
- {
- List<int> nums = new List<int>();
- using (StreamReader reader = new StreamReader("numbers.txt"))
- {
- string line;
- while ((line = reader.ReadLine()) != null)
- {
- nums.Add(int.Parse(line));
- }
- }
- BubbleSort(nums);
- Console.WriteLine(string.Join(", ", nums));
- using (StreamWriter writer = new StreamWriter("numberssort.txt"))
- {
- foreach (var item in nums)
- {
- writer.WriteLine(item);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement