jsソートを実現する(継続更新)

2291 ワード

    let array = [24,1,6,45,23,11,2,34,8,54,14];
       /*
       //       O(n2)
       for (let i=0;ij;k--){
               array[k] = array[k-1];
             }
             array[j] = siwt;
           } 
         }
       }
       */

       //      
      /*
         
      for(i in arr.length){
         
      }
      */
       for (let i=0;iarray[i]?middle:(middle+1);
         let dist = array[i];
         for(let j=i;j>middle;j--){
           array[j]=array[j-1];
         }
         array[middle] = dist;
       }
       console.log([...array]);

       //    
              
       //    
       /*
          
       quickSort(low,high){
         let middle = (low+high)/2
         let i=low,j=high;
         while(iarr[middle]){
             exchange
           }
           while(arr[j--]=high) return;
         middle = quickSort(low,high)//middle                
         sort(low,middle)
         sort(middle+1,low)
       }
       */
       let i = 0;
       let quickSort = (low,high)=>{
         let middle = Math.floor((low+high)/2);
         console.log(`low:${low},high:${high},middle:${middle}`);
         let i = low;
         let j = high;
         while(iarray[middle]){
               console.log(`before : ${array[i]},${array[middle]}`);
               let dist = array[i];
               array[i] = array[middle];
               array[middle] = dist;
               middle = i;
               i++;
               break;
             }
             i++;
           }
           while(j>middle){
             if(array[j]{
         console.log([...array]);
         let middle = quickSort(low,high);
         if (low>=high) {
             return;
         }
         mainQuickSort(low,middle);
         mainQuickSort(middle+1,high);
       };
       mainQuickSort(0,array.length-1);