while (l <= mid && r <= right) {if (items[l].val> items[r].val) { // l > r, means move r to l, so insert r data to temp, // and add rightcount , ex 5 2 => 2 5, but rightCount should++ rightCount++; temp[tempIdx++] = items[r++]; } else { // if l < r, it is sorted, ex: 2 5, // cal count result(count[l index] += rightCnt), // insert l data to temp count[items[l].idx] += rightCount; temp[tempIdx++] = items[l++]; } }
็ฌฌไบๅ while ๅฐฑ่ท ็ฌฌไธๅ while else ๅ ถๅฏฆๅ งๅฎนไธๆจฃ ๏ผ ๅ ็บๆฏ l <= mid)
while (l <= mid) { // insert remained l data, and cal count, (insert l data means cal result!) count[items[l].idx] += rightCount; temp[tempIdx++] = items[l++]; }
ex 5 2 .=> 2, 5 ไนๅพ, ้ๅฉไธ 5, ๆไปฅๅก 5 ๅฐ temp[1] .( while l <= mid
while (l <= mid) { // insert remained l data, and cal count, (insert l data means cal result!) count[items[l].idx] += rightCount; temp[tempIdx++] = items[l++]; }while (r <= right) { // insert remained r data temp[tempIdx++] = items[r++]; }// finally put sorted temp data to itemsfor (int p =0; p <temp.length; p++) { items[left + p] = temp[p]; }