This project implements a merge function that combines three sorted arrays into a single sorted array. The function handles the following conditions:
collection_1
andcollection_3
are sorted in ascending ordercollection_2
is sorted in descending order
- Node.js (v14 or higher)
- npm (Node Package Manager)
- Clone the repository:
git clone https://github.com/yourusername/merge-sort.git
cd merge-sort
- Install dependencies:
npm install
merge-sort/
├── src/
│ ├── index.ts # Main entry point with example usage
│ ├── merge.ts # Merge function implementation
│ └── merge.test.ts # Unit tests
├── dist/ # Compiled JavaScript files
├── package.json # Project configuration and dependencies
├── tsconfig.json # TypeScript configuration
├── jest.config.js # Jest test configuration
└── README.md # Project documentation
To run the unit tests:
npm test
To compile the TypeScript code:
npm run build
To run the example code:
npm start
This will execute the example in src/index.ts
which demonstrates the merge function with sample arrays.
merge(collection1: number[], collection2: number[], collection3: number[]): number[]
collection1
: Array of numbers sorted in ascending ordercollection2
: Array of numbers sorted in descending ordercollection3
: Array of numbers sorted in ascending order
- A new array containing all elements from the input arrays, sorted in ascending order
import { merge } from './merge';
const collection1 = [1, 3, 5]; // ascending
const collection2 = [6, 4, 2]; // descending
const collection3 = [0, 7, 8]; // ascending
const result = merge(collection1, collection2, collection3);
console.log(result); // Output: [0, 1, 2, 3, 4, 5, 6, 7, 8]
The implementation is thoroughly tested with the following test cases:
test('should merge three sorted arrays correctly', () => {
const collection1 = [1, 3, 5]; // ascending
const collection2 = [6, 4, 2]; // descending
const collection3 = [0, 7, 8]; // ascending
const result = merge(collection1, collection2, collection3);
expect(result).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8]);
});
test('should handle empty arrays', () => {
const collection1: number[] = [];
const collection2: number[] = [3, 2, 1];
const collection3: number[] = [];
const result = merge(collection1, collection2, collection3);
expect(result).toEqual([1, 2, 3]);
});
test('should handle arrays of different lengths', () => {
const collection1 = [1, 5]; // ascending
const collection2 = [6, 4, 2]; // descending
const collection3 = [3]; // ascending
const result = merge(collection1, collection2, collection3);
expect(result).toEqual([1, 2, 3, 4, 5, 6]);
});
test('should handle arrays with duplicate values', () => {
const collection1 = [1, 2, 2]; // ascending
const collection2 = [3, 2, 1]; // descending
const collection3 = [2, 3, 3]; // ascending
const result = merge(collection1, collection2, collection3);
expect(result).toEqual([1, 1, 2, 2, 2, 2, 3, 3, 3]);
});
- Type-safe implementation using TypeScript
- Comprehensive unit tests
- No use of built-in sort functions
- Handles edge cases:
- Empty arrays
- Arrays of different lengths
- Arrays with duplicate values