Форум программистов, компьютерный форум, киберфорум
C# для начинающих
Войти
Регистрация
Восстановить пароль
Карта форума Темы раздела Блоги Сообщество Поиск Заказать работу  
 
0 / 0 / 0
Регистрация: 01.02.2014
Сообщений: 27
1

Ошибка в интроспективной сортировке массива целых чисел по убыванию

02.06.2016, 21:27. Показов 376. Ответов 1
Метки нет (Все метки)

Author24 — интернет-сервис помощи студентам
Пытаюсь сделать сортировку большого массива целых чисел по убыванию следующим образом:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 
namespace introSort
{
 
    class Program
    {
        public static void IntrospectiveSorting(ref int[] array)
        {
            int sizeOfPartition = Partitioning(ref array, 0, array.Length - 1);
 
            if (sizeOfPartition < 16)
            {
                InsertionSorting(ref array);
            }
            else if (sizeOfPartition > (2 * Math.Log(array.Length)))
            {
                HeapSorting(array);
            }
            else
            {
                QuickRecursiveSorting(ref array, 0, array.Length - 1);
            }
        }
 
        private static void InsertionSorting(ref int[] array)
        {
            for (int i = 1; i < array.Length; ++i)
            {
                int j = i;
 
                while ((j > 0))
                {
                    if (array[j - 1] < array[j])
                    {
                        array[j - 1] ^= array[j];
                        array[j] ^= array[j - 1];
                        array[j - 1] ^= array[j];
 
                        --j;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        static void MinHeapCreating(int[] input)
        {
            input[0] = input.Length - 1;
 
            for (int i = (input[0] / 2); i > 0; i--)
            {
                MinHeapify(input, i);
            }
        }
 
        static void MinHeapify(int[] input, int index)
        {
            var left = 2 * index;
            var right = 2 * index + 1;
            int smallest;
 
            if (left <= input[0] && input[left] < input[index])
                smallest = left;
            else
                smallest = index;
 
            if (right <= input[0] && input[right] < input[smallest])
                smallest = right;
 
            if (smallest != index)
            {
                Swaping(ref input[smallest], ref input[index]);
                MinHeapify(input, smallest);
            }
        }
 
        static public void HeapSorting(int[] input)
        {
            MinHeapCreating(input);
 
            for (int i = input[0]; i > 0; i--)
            {
                Swaping(ref input[1], ref input[i]);
                input[0]--;
                MinHeapify(input, 1);
            }
        }
 
        static public void Swaping(ref int a, ref int b)
        {
            var temp = a;
            a = b;
            b = temp;
        }
 
 
        private static void QuickRecursiveSorting(ref int[] array, int left, int right)
        {
            if (left > right)
            {
                int q = Partitioning(ref array, left, right);
                QuickRecursiveSorting(ref array, left, q - 1);
                QuickRecursiveSorting(ref array, q + 1, right);
            }
        }
 
        private static int Partitioning(ref int[] array, int left, int right)
        {
            int root = array[right];
            int temp;
            int i = left;
 
            for (int j = left; j < right; ++j)
            {
                if (array[j] <= root)
                {
                    temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                    ++i;
                }
            }
 
            array[right] = array[i];
            array[i] = root;
 
            return i;
        }
        static void Main(string[] args)
        {
            int[] array = { 3, 4, 50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 
                            4, 20, 111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20,
                            4, 20, 111, 123, 4, 50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10,
                            5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10,
                            5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 
                            20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8,
                            20, 304, 10, 2, 30, 4, 05, 660, 234, 102, 5053, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4,
                            0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 
                            5, 2, 3, 7, 8, 20, 304, 10, 2, 30, 4, 05, 660, 234, 102, 50, 50, 100, 200, 0, 10, 20, 30, 500, 4, 50, 100, 200, 0, 10, 20, 30, 5004, 50, 100, 200, 
                            0, 10, 20, 30, 500, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 100, 
                            200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 
                            23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 4,
                            50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20,
                            111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20,
                            111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 
                            123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8, 20, 304, 10, 2, 30, 
                            4, 05, 660, 234, 102, 5053, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 
                            4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8, 20, 304, 10, 
                            2, 30, 4, 05, 660, 234, 102, 50, 50, 100, 200, 0, 10, 20, 30, 500, 4, 50, 100, 200, 0, 10, 20, 30, 5004, 50, 100, 200, 0, 10, 20, 30, 500, 7, 2, 3, 
                            4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56 };
            IntrospectiveSorting(ref array);
            for (int i = 0; i < array.Length - 1; i++)
            {
 
                if (i % 7 == 0 && i != 0)
                    Console.Write("\n");
                Console.Write(array[i]);
                Console.Write(' ');
            }
 
        }
    }
 
}
Она работает неверно т. к. в качестве первого элемента массива получается ноль. Как это происходит для меня какая-то загадка не смотря на часы попыток разобраться с этим ((( Может кто-нибудь сможет понять как сюда закралась эта ошибка? Заранее огромная благодарность.
P. S. Сдвинуть массив на элемент влево не предлагать т. к. это критично для времени. И еще наверно было бы круто если бы подсказали как лучше распараллелить QuickRecursiveSorting.
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
02.06.2016, 21:27
Ответы с готовыми решениями:

Ошибка в сортировке массива по убыванию
Подскажите в чем ошибка, сортирую массив по убыванию #include &lt;iostream&gt; #include &lt;ctime&gt;...

Отсортировать строки массива целых чисел по убыванию.
Решите пожалуйста быстро задачу,по теме двумерные массивы!

Отсортировать строки массива целых чисел по убыванию
Отсортировать строки массива целых чисел по убыванию. Сортировка включением. Пожалуйста, не...

Отсортировать строки массива целых чисел по убыванию.
Отсортировать строки массива целых чисел по убыванию.

1
91 / 90 / 37
Регистрация: 05.08.2011
Сообщений: 428
03.06.2016, 01:24 2
Vi Sparks, Абсолютно никакой загадки. 54 и 90 строки делают своё дело.
Исправил, не знаю как и что это за методы, но работает:
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
 
namespace introSort
{
 
    class Program
    {
        public static void IntrospectiveSorting(ref int[] array)
        {
            int sizeOfPartition = Partitioning(ref array, 0, array.Length - 1);
 
            if (sizeOfPartition < 16)
            {
                InsertionSorting(ref array);
            }
            else if (sizeOfPartition > (2 * Math.Log(array.Length)))
            {
                HeapSorting(array);
            }
            else
            {
                QuickRecursiveSorting(ref array, 0, array.Length - 1);
            }
        }
 
        private static void InsertionSorting(ref int[] array)
        {
            for (int i = 1; i < array.Length; ++i)
            {
                int j = i;
 
                while ((j > 0))
                {
                    if (array[j - 1] < array[j])
                    {
                        array[j - 1] ^= array[j];
                        array[j] ^= array[j - 1];
                        array[j - 1] ^= array[j];
 
                        --j;
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
        static void MinHeapCreating(int[] input,int first)
        {
 
            for (int i = (first / 2); i > 0; i--)
            {
                MinHeapify(input, i,first);
            }
        }
 
        static void MinHeapify(int[] input, int index,int first)
        {
            var left = 2 * index;
            var right = 2 * index+1;
            int smallest;
 
            if (left <= first && input[left] < input[index])
                smallest = left;
            else
                smallest = index;
 
            if (right <= first && input[right] < input[smallest])
                smallest = right;
 
            if (smallest != index)
            {
                Swaping(ref input[smallest], ref input[index]);
                MinHeapify(input, smallest,first);
            }
        }
 
        static public void HeapSorting(int[] input)
        {
            int first = input.Length-1;
 
            MinHeapCreating(input,first);
            for (int i = first; i > 0; i--)
            {
                Swaping(ref input[0], ref input[i]);
                first--;
                MinHeapify(input, 0,first);
            }
        }
 
        static public void Swaping(ref int a, ref int b)
        {
            var temp = a;
            a = b;
            b = temp;
        }
 
 
        private static void QuickRecursiveSorting(ref int[] array, int left, int right)
        {
            if (left > right)
            {
                int q = Partitioning(ref array, left, right);
                QuickRecursiveSorting(ref array, left, q - 1);
                QuickRecursiveSorting(ref array, q + 1, right);
            }
        }
 
        private static int Partitioning(ref int[] array, int left, int right)
        {
            int root = array[right];
            int temp;
            int i = left;
 
            for (int j = left; j < right; ++j)
            {
                if (array[j] <= root)
                {
                    temp = array[j];
                    array[j] = array[i];
                    array[i] = temp;
                    ++i;
                }
            }
 
            array[right] = array[i];
            array[i] = root;
 
            return i;
        }
        static void Main(string[] args)
        {
            int[] array = { 3, 4, 50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 
                            4, 20, 111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20,
                            4, 20, 111, 123, 4, 50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10,
                            5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10,
                            5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 
                            20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8,
                            20, 304, 10, 2, 30, 4, 05, 660, 234, 102, 5053, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4,
                            0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 
                            5, 2, 3, 7, 8, 20, 304, 10, 2, 30, 4, 05, 660, 234, 102, 50, 50, 100, 200, 0, 10, 20, 30, 500, 4, 50, 100, 200, 0, 10, 20, 30, 5004, 50, 100, 200, 
                            0, 10, 20, 30, 500, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 100, 
                            200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 
                            23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 4,
                            50, 100, 200, 0, 10, 20, 30, 500, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20,
                            111, 123, 53, 23, 778, 45, 3, 400, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20,
                            111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 4, 20, 111, 
                            123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8, 20, 304, 10, 2, 30, 
                            4, 05, 660, 234, 102, 5053, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56, 45, 2, 6, 0, 2, 6, 7, 2, 3, 4, 0, 10, 5, 67, 20, 
                            4, 20, 111, 123, 53, 23, 778, 45, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 845, 3, 67, 8, 34, 8, 3, 4, 5, 2, 3, 7, 867, 8, 34, 8, 3, 4, 5, 2, 3, 7, 8, 20, 304, 10, 
                            2, 30, 4, 05, 660, 234, 102, 50, 50, 100, 200, 0, 10, 20, 30, 500, 4, 50, 100, 200, 0, 10, 20, 30, 5004, 50, 100, 200, 0, 10, 20, 30, 500, 7, 2, 3, 
                            4, 0, 10, 5, 67, 20, 4, 20, 111, 123, 53, 23, 778, 0, 10, 302, 10, 0, 1, 20, 3, 5, 345, 2, 12, 345, 456, 3, 56 };
            IntrospectiveSorting(ref array);
            for (int i = 0; i < array.Length - 1; i++)
            {
 
                if (i % 7 == 0 && i != 0)
                    Console.Write("\n");
                Console.Write(array[i]);
                Console.Write(' ');
            }
            Console.ReadKey();
        }
    }
 
}
0
03.06.2016, 01:24
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
03.06.2016, 01:24
Помогаю со студенческими работами здесь

Массив: Отсортировать строки массива целых чисел по убыванию...
Отсортировать строки массива целых чисел по убыванию.

Массив: Отсортировать строки массива целых чисел по убыванию.
Отсортировать строки массива целых чисел по убыванию. Простым кодом пожалуйста

Отсортировать строки массива целых чисел по убыванию. Шейкерная сортировка
Отсортировать строки массива целых чисел по убыванию. Шейкерная сортировка

Отсортировать элементы нечетных строк массива целых чисел по убыванию
Отсортировать элементы нечетных строк массива целых чисел по убыванию. Сортировка прямой выбор.


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
2
Ответ Создать тему
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2024, CyberForum.ru