using System;using System.Collections.Generic;publicclassKata{publicstaticint[]SortArray(int[] array){
List<int> odd =newList<int>();foreach(var number in array){if(number %2!=0)
odd.Add(number);}int[] oddAr = odd.ToArray();
Array.Sort(oddAr);for(int i =0, j =0; i<array.Length; i++){if(array[i]%2!=0){
array[i]= oddAr[j];
j++;}}return array;}}
答案3:
using System;using System.Collections.Generic;using System.Linq;publicclassKata{publicstaticint[]SortArray(int[] array){var list =newList<int>(array);var oddNumbers = list.Where(x => x %2==1).OrderBy(x => x).ToList();var oddIndex =0;return list.Select(x =>(x %2==1)? oddNumbers[oddIndex++]: x).ToArray();}}
答案4:
using System.Linq;publicclassKata{publicstaticint[]SortArray(int[] array){int[] sortedOddNumbers = array.Where(x => x %2==1).OrderBy(x => x).ToArray();int i =0;foreach(int oddNumber in sortedOddNumbers){while(array[i]%2==0){
i++;}
array[i]= oddNumber;
i++;}return array;}}
答案5:
using System.Collections.Generic;using System.Linq;publicclassKata{publicstaticint[]SortArray(int[] array){
Queue<int> sortedOddNumbers =newQueue<int>(array.Where(i =>IsOdd(i)).OrderBy(i => i));return array.Select(i =>IsOdd(i)? sortedOddNumbers.Dequeue(): i).ToArray();}privatestaticboolIsOdd(int i){return i %2==1;}}
答案6:
using System;using System.Collections.Generic;publicclassKata{publicstaticint[]SortArray(int[] array){if(array.Length ==0)return array;for(int i=0;i<array.Length-1;i++){if(array[i]%2==0)continue;for(int j=i+1;j<array.Length;j++){if(array[j]%2==0)continue;if(array[j]<array[i]){int aux = array[i];
array[i]= array[j];
array[j]= aux;}}}return array;}}
答案7:
using System;using System.Linq;using System.Collections.Generic;publicclassKata{publicstaticint[]SortArray(int[] array){var odds = array.Where(x => x %2!=0).ToArray();//create new array of odds
Array.Sort(odds);var ans =newList<int>(odds);//convert into list so it can be modified foreach(var item in array.Select((Value, Index)=>new{Value, Index})//instantiate val and index reference for each item .Where(x => x.Value %2==0))//filter evens{if(item.Index > ans.Count()-1) ans.Add(item.Value);//if index in orig array is out of bonds of the current list, add it to the endelse ans.Insert(item.Index, item.Value);//else, insert it at the index in orig arraay}return ans.ToArray();}}
答案8:
using System;publicclassKata{publicstaticint[]SortArray(int[] array){int len = array.Length, oddsIdx =0;int[] odds =newint[len],
indexes =newint[len];for(int i =0; i < len;i++)if(array[i]%2!=0)
odds[oddsIdx]= array[(indexes[oddsIdx++]= i)];
Array.Sort(odds);for(int i =(len - oddsIdx), n =0; i < len; i++, n++)
array[indexes[n]]= odds[i];return array;}}
答案9:
using System;publicclassKata{publicstaticint[]SortArray(int[] array){int len = array.Length;int[] odds =newint[len];for(int i =0, oddsIdx =0, tempInt =0; i < len; i++)if((tempInt = array[i])%2!=0){
array[i]=-1;
odds[oddsIdx++]= tempInt;}
Array.Sort(odds);for(int i =--len, oddsIdx = len; i >-1; i--)if(array[i]==-1) array[i]= odds[oddsIdx--];return array;}}
答案10:
publicclassKata{publicstaticint[]SortArray(int[] array){for(int i =0; i < array.Length; i++){if(array[i]%2!=0){var aux =0;for(int j = i +1; j < array.Length; j++){if(array[i]> array[j]&& array[j]%2!=0){
aux = array[i];
array[i]= array[j];
array[j]= aux;}}}}return array;}}