0% found this document useful (0 votes)
39 views3 pages

16 Challenge Implement Swap

The document describes implementing a swap function that exchanges the elements at two specified indices of an integer array. It provides the function prototype, sample input and output, and explains that the goal is to return the input array with the integers at the given indices swapped. It prompts coding the solution as an exercise and provides an empty function template to fill in for swapping array elements based on the given indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

16 Challenge Implement Swap

The document describes implementing a swap function that exchanges the elements at two specified indices of an integer array. It provides the function prototype, sample input and output, and explains that the goal is to return the input array with the integers at the given indices swapped. It prompts coding the solution as an exercise and provides an empty function template to fill in for swapping array elements based on the given indices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Challenge: Implement Swap

Do you know how Swap Function works? Implement to prove yourself!

WE'LL COVER THE FOLLOWING

• Swap Function *
• Function Prototype:
• Output:
• Sample Input
• Sample Output
• Explanation
• Coding Exercise

Swap Function #
A key step in many sorting algorithms (including selection sort) is swapping
the location of two items in an array. Here’s an Empty Function that needs to
Swap the elements of a given array, as per the specified indices.

Function Prototype: #

public static void swap(int[] array, int firstIndex, int secondIndex);

where array is the input int array and firstIndex/ secondIndex specify the
integers to swap.

Output: #

Returns the Updated Array

Sample Input #
arr1 = [9,4,7,1,2,6,5]

firstIndex = 2
secondIndex = 0

Sample Output #

arr1 = [7,4,9,1,2,6,5]

Explanation #

You are provided with an array of integers, along with the first and second
index. You need to return the input array with the integers at the position of
firstIndex and secondIndex Swapped!

Coding Exercise #
Take a close look and design a step-by-step algorithm first before jumping on
implementation. This problem is designed for your practice, so try to solve it
on your own first. If you get stuck, you can always refer to the solution
provided in the solution section. Good Luck!

Java

class Solution {
public static void swap(int[] array, int firstIndex, int secondIndex) {

return;
}
}

You might also like