-2

Given an array, return a sorted array. Further, what if only a pair of consecutive numbers are to be sorted without considering the duplicates? What if instead of a pair, its triplets? Can you form a generic solution? Mention time complexity for each case.

Input: [4, 2, 5, 6, 3, 1, 3, 2, 5]

Output for simple sort: [1, 2, 2, 3, 3, 4, 5, 5, 6]

Output for pair-sorting: [2, 4, 5, 6, 1, 3, 2, 3, 5]

Output for triplet-sorting: [2, 4, 5, 1, 3, 6, 2, 3, 5]

Explanation of simple sort - Its simple sort and align it in line

Explanation of pair-sorting - it picks in pair-wise of 2 elements example from input first pair 4,2 and sort 2,4 and then second pair 5,6 and sort to 5,6, and then 3,1 and sort to 1,3 and so on. Pair wise sorting.

Explanation of triplet-sorting - This pick three elements and sort example from input first 3 elements 4,2,5 and sort to 2,4,5 and so onn...

I want a generic function that takes input of array And type of sort - Example - simple, pair or triplet and return the result accordingly

int array[] = {4, 2, 5, 6, 3, 1, 3, 2, 5}

for(int i =0; i < array.length; i = i+2) {

    int temp = array[i];
    array[i] = array[i+1];
    array[i+1] = temp; 
}

for(int i = 0; i < array.length; i++){
    System.out.println(array[i]);
}

So far I tried this. But unable to achieve any outcome. Can anyone help me with this? Pseudocode or any language will work. I need to come up with solution.

trincot
  • 317,000
  • 35
  • 244
  • 286
Tech Nerd
  • 93
  • 3
  • 11
  • What syntax is this? :\ – Timothy Alexis Vass Jul 29 '21 at 20:23
  • 1
    Are you missing a '3' in your pair-sorting? – Surt Jul 29 '21 at 21:18
  • A question with the same "assignment" (first paragraph) was posted earlier today, but [deleted](https://stackoverflow.com/questions/68576119/best-way-to-sort-an-array-with-triplet?noredirect=1#comment121196441_68576119). It had the same 3 problem. Weird... – trincot Jul 29 '21 at 21:42
  • *"without considering the duplicates"*: what does this mean? How does this impact the result? – trincot Jul 29 '21 at 21:46
  • Well, the latest update of the question removes all details, and makes it a non-question; Rolled back. Show some respect for this site. – trincot Jul 30 '21 at 05:46

2 Answers2

1

Here is a solution with JavaScript.

So what it does it that for simple sort, it simply uses the normal sorting function for the JavaScript Array object.

For pair- and triplet-sorting it loops through the array in pairs/triplets and makes a temporary array which is sorted and reinserted.

let array = [4, 2, 5, 6, 3, 1, 3, 2, 5];

const NUMERICALLY = (a,b) => a - b;

function sorting(input_array, method) {
  // deep-clone input_array to keep input_array intact:
  let array = JSON.parse(JSON.stringify(input_array));

  if (method == "simple sort") {
    array.sort(NUMERICALLY);
  }

  else if (method == "pair-sorting") {
    for (let a = 0; a < array.length-1; a += 2) {
      let b = a+1;
      let tempArray = [array[a], array[b]]
      tempArray.sort(NUMERICALLY);
      array[a] = tempArray[0];
      array[b] = tempArray[1];
    }
  }

  else if (method == "triplet-sorting") {
    for (let a = 0; a < array.length-2; a += 3) {
      let b = a+1;
      let c = b+1;
      let tempArray = [array[a], array[b], array[c]]
      tempArray.sort(NUMERICALLY);
      array[a] = tempArray[0];
      array[b] = tempArray[1];
      array[c] = tempArray[2];
    }
  }

  else {
    console.error("method needs to be either 'simple sort', 'pair-sorting' or 'triplet-sorting'");
  }
  return array;
}

console.log("Simple sort: " + sorting(array, "simple sort"));
console.log("Pair-sorting: " + sorting(array, "pair-sorting"));
console.log("Triplet-sorting: " + sorting(array, "triplet-sorting"));
Timothy Alexis Vass
  • 2,526
  • 2
  • 11
  • 30
1

I solved this using Python3: I created a function that accepts a list, and n (the type of sort). Pass 0 or 1 for a simple sort, 2 for pair sort, 3 for triple sort, but it will work for any number assuming it is smaller than the size of the list.

from math import ceil
def simpleSort(myList, n: int):
    if n == 0 or n==1:
        myList.sort()
        return myList
    else:
        new_list = []
        for sub_list_index in range(ceil(len(myList)/n)):
            sub_list = myList[(sub_list_index) * n:(sub_list_index + 1) * n]
            sub_list.sort()
            for digit in sub_list:
                if digit:
                    new_list.append(digit)
        return new_list
                

print(simpleSort([4, 2, 5, 6, 3, 1, 3, 2, 5], 0))  # --> [1, 2, 2, 3, 3, 4, 5, 5, 6]
print(simpleSort([4, 2, 5, 6, 3, 1, 3, 2, 5], 1))  # --> [1, 2, 2, 3, 3, 4, 5, 5, 6]
print(simpleSort([4, 2, 5, 6, 3, 1, 3, 2, 5], 2))  # --> [2, 4, 5, 6, 1, 3, 2, 3, 5]
print(simpleSort([4, 2, 5, 6, 3, 1, 3, 2, 5], 3))  # --> [2, 4, 5, 1, 3, 6, 2, 3, 5]

Python's built in sort function for lists makes this a fairly simple problem. The first nested for loop is calculated by counting how many times n is in the length of the list. Once you know how many times you will have to go through the list, you can break the list into sub-lists. Sort each sub-list and add them to a new list.

I also noticed your pair-sort expected output cut out the last digit. If that is the expected behavior, you can easily add a floor() instead of a ceil() for even n values.

Jeff Gruenbaum
  • 364
  • 6
  • 21
  • 1
    Are you sure the pair-sorting is correct, in the OP's result there is only one 3 and 8 total numbers? – Surt Jul 29 '21 at 21:16
  • I didn't account for this in my function, so it is incorrect. It can easily be fixed by adding a if ```if floor(ceil(len(myList)/n) % 2 == 0``` and changing the logic from there. – Jeff Gruenbaum Jul 30 '21 at 13:47