Kaos
Kaos
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
You can shorten the body of the if/else incrementing to be like my while statements, or expand the while statement, but they should be consistent either way. If you have any questions, feel free to ping
10 replies
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
since I know that the a index and the b index are going to be less than C's length, I can break as soon as one of those is finished. Then I can add whatever the remaining one has left in it. If A is exhausted, then aIndex < A.Length will be false and that else will be skipped. Then it will add the rest of what is in B.
10 replies
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
You could do something like this
using System;

Random random = new Random();
int N = 7;// random.Next(5, 10);
int M = 6;// random.Next(5, 10);
//int[] A = new int[N];
//int[] B = new int[M];
int[] A = { 1, 2, 5, 6, 10, 11, 13 };
int[] B = { 3, 8, 12, 15, 20, 30 };

//for (int i = 0; i < N; i++)
//{
// A[i] = random.Next(-30,50);
//}
//for (int i = 0; i < M; i++)
//{
// B[i] = random.Next(-30, 50);
//}

Array.Sort(A);
Array.Sort(B);

Console.WriteLine("Первый отсортированный массив");
int i;

for (i = 0; i < N; i++)
{
Console.Write(A[i] + "\t");
}

Console.WriteLine();

Console.WriteLine("Второй отсортированный массив");
for (i = 0; i < M; i++)
{
Console.Write(B[i] + "\t");
}
int Y = M + N;
int[] C = new int[Y];
int aIndex = 0;
int bIndex = 0;
i = 0;

Console.WriteLine();
while( aIndex < A.Length && bIndex < B.Length)
{
if (A[aIndex] > B[bIndex])
{
C[i] = B[bIndex];
bIndex++;
i++;
}
else
{
C[i] = A[aIndex];
aIndex++;
i++;
}
}

while(aIndex < A.Length)
C[i++] = A[aIndex++];
while(bIndex < B.Length)
C[i++] = B[bIndex++];


Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
for (i = 0; i < Y; i++)
{
Console.Write(C[i] + "\t");
}
Console.WriteLine();
// code here
using System;

Random random = new Random();
int N = 7;// random.Next(5, 10);
int M = 6;// random.Next(5, 10);
//int[] A = new int[N];
//int[] B = new int[M];
int[] A = { 1, 2, 5, 6, 10, 11, 13 };
int[] B = { 3, 8, 12, 15, 20, 30 };

//for (int i = 0; i < N; i++)
//{
// A[i] = random.Next(-30,50);
//}
//for (int i = 0; i < M; i++)
//{
// B[i] = random.Next(-30, 50);
//}

Array.Sort(A);
Array.Sort(B);

Console.WriteLine("Первый отсортированный массив");
int i;

for (i = 0; i < N; i++)
{
Console.Write(A[i] + "\t");
}

Console.WriteLine();

Console.WriteLine("Второй отсортированный массив");
for (i = 0; i < M; i++)
{
Console.Write(B[i] + "\t");
}
int Y = M + N;
int[] C = new int[Y];
int aIndex = 0;
int bIndex = 0;
i = 0;

Console.WriteLine();
while( aIndex < A.Length && bIndex < B.Length)
{
if (A[aIndex] > B[bIndex])
{
C[i] = B[bIndex];
bIndex++;
i++;
}
else
{
C[i] = A[aIndex];
aIndex++;
i++;
}
}

while(aIndex < A.Length)
C[i++] = A[aIndex++];
while(bIndex < B.Length)
C[i++] = B[bIndex++];


Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
for (i = 0; i < Y; i++)
{
Console.Write(C[i] + "\t");
}
Console.WriteLine();
// code here
10 replies
CC#
Created by Mute on 3/9/2024 in #help
The program should connect 2 sorted arrays into 1 sorted array
it seems like you are trying to track if one of the arrays are finished but, you don't actually need those variables
10 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
do take a break, but the key thing I said is with the index
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
What I would do, is compare the first elements of each. Then I would compare the second element of one to the first of the other
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
right, but if you were going to do that by hand, what would you do?
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
they both should be ordered already, since, at first you are building the output from a single input
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
right, I don't want to tell you the answer, it is counter productive for you
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
they both may be ordered, so I don't really want to use that term
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
you can start at the beginning, but move the index variable instead of chopping off the first item always
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
so, instead of tracking the first element of a list, what if you track the first non-processed element?
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
I understand what merge sort does. But is there any other way to track elements in a finite amount of collections?
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
of merging two collections?
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
This is the meat of that method
List<T> list = new List<T>(count);
Array.Copy(_items, index, list._items, 0, count);
list._size = count;
return list;
List<T> list = new List<T>(count);
Array.Copy(_items, index, list._items, 0, count);
list._size = count;
return list;
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
whenever you are doing .GetRange, you are creating a new list and giving the GC more work
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
instead of get range, tracking where you are in both collections would teach you more
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
while (left.Count > 0 && right.Count > 0)
{
if (left[0] <= right[0])
{
output.Add(left[0]);
left = left.GetRange(1, left.Count - 1);
}
else
{
output.Add(right[0]);
right = right.GetRange(1, right.Count - 1);
}
}
while (left.Count > 0 && right.Count > 0)
{
if (left[0] <= right[0])
{
output.Add(left[0]);
left = left.GetRange(1, left.Count - 1);
}
else
{
output.Add(right[0]);
right = right.GetRange(1, right.Count - 1);
}
}
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
one thing I do notice is that you you are doing a lot with list, but it would be a lot better (and teach you a lot more) if you were doing single element references and logic
48 replies
CC#
Created by MyriadColors on 4/19/2023 in #help
❔ Weird performance behavior with sorting algorithms.
that is the worst case for quick sort, so it should be VERY slow, but Merge sort shouldn't be that affected
48 replies