How to sort ascending only odd numbers in array?

Multi tool use
Multi tool use


How to sort ascending only odd numbers in array?



I want to sort only odd numbers without moving even numbers. For example, if my input is:


[5, 3, 2, 8, 1, 4]



The expected result is:


[1, 3, 2, 8, 5, 4]



I am new to C# and I came across a challenge on the Internet that has me perplexed. I have tried for hours and I would like to learn this concept in



The challenge states:



You have an array of numbers. Your task is to sort ascending odd numbers but even numbers must be on their places. Zero isn't an odd number and you don't need to move it. If you have an empty array, you need to return it.



Here is my code so far, please take it easy on me I am in the beginning stages of programming.


public static int SortArray(int array)
{
var dict = new Dictionary<int, int>();
var dict2 = new Dictionary<int, int>();

for (int i = 0; i < array.Length; i++)
{
int j =0;
if (array[i] % 2 != 0)
{
dict.Add(array[i], i+1);
}
else
{
dict2.Add(array[i], i+1);
}
}
var result = dict.OrderBy(x => x.Key);
Dictionary<int, int> resultDic = result.Union(dict2)
.GroupBy(x => x.Key).ToDictionary(o => o.Key, o => o.Key);
}

public static void Main()
{
SortArray(new int { 5, 3, 2, 8, 1, 4});
}





Seems you should be able to use any existing in-place sorting algorithm - just completely ignore (step over) positions that hold even values.
– 500 - Internal Server Error
27 mins ago





Welcome to StackOverflow; I notice that there is no question in your question, just a block of code and a statement of a problem. What is your specific question? If your code works and you'd like a critique, post it on the code review site, not this site. If your code does not work then ask a specific question about the problem.
– Eric Lippert
1 min ago





1 Answer
1



Check this code. Explanations added as comments


public static int SortArray(int array)
{
//temp variable for holding larger value for switching
int temp = 0;

for (int i = 0; i < array.Length; i++)
{
//If the value is 'even' continue with outer loop
if(array[i] % 2 == 0)
continue;

//Inner loop to compare array values
for(int j = (i + 1); j < array.Length; j++)
{
//If this value is not even do comparison
if(array[j] % 2 != 0)
{
//If the left value is greater than the right value
//swap them
if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}

return array;
}

public static void Main()
{
SortArray(new int { 5, 3, 2, 8, 1, 4});
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

ufE,B7w0Yc6AiXwO74KTASuBA gFpca9eCSBpWOz8CWXLI,dDaBOs mfYhpxjz 2
o4 JF6lv9KYyMIhZaE4vxvndY4WjjEPx5rrzfL zZrlXDJXE37I8xM8,QBdc9xOGqRaPU46piuawWYLW6Ztpj,xp6KJmjB G

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters