Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Assume we have a list of integers, for example:
L = [13,13,4,13,4,2]
I want to find the set of all palindromes, where each palindrome is a sub-list of L containing contiguous integers. For the above list that would be:
S = {[13], [4], [2], [13,13], [13,4,13], [4,13,4]}
Because the inverse of L would be L' = [2,4,13,4,13,13], and every element of S appears in L' in the correct order.
How can I find the set of all palindromes in general? My naive approach would be to check if each element of the power set of L appears in L', but this is inefficient and I am sure that there is a better solution.
I think my solution is pretty similar to solution from MC Emperor, but I focused on not creating temporary objects like lists.
I select sub-arrays of given array using left and right indices and check it for palindrome.
public static Set<List<Integer>> findAllPalindromes(int[] arr) {
Set<List<Integer>> res = new LinkedHashSet<>();
for (int length = 1; length < arr.length; length++)
for (int left = 0, right = left + length - 1; right < arr.length; left++, right++)
if (isPalindrome(arr, left, right))
res.add(sublist(arr, left, right));
return res;
}
This method check is given sub-array palindrome or not:
private static boolean isPalindrome(int[] arr, int left, int right) {
for (; left < right; left++, right--)
if (arr[left] != arr[right])
return false;
return true;
}
This method create separate list for given sub-array:
private static List<Integer> sublist(int[] arr, int left, int right) {
List<Integer> res = new ArrayList<>(right - left);
for (; left <= right; left++)
res.add(arr[left]);
return res;
}
You need two steps to do that.
First, you'll need to find all sublists within the list:
List<Integer> input = Arrays.asList(13, 13, 4, 13, 4, 2);
List<List<Integer>> subLists = new ArrayList<>();
for (int subListSize = 1; subListSize < input.size(); subListSize++) {
for (int startIndex = 0; startIndex < input.size() - subListSize + 1; startIndex++) {
List<Integer> subList = input.subList(startIndex, startIndex + subListSize);
subLists.add(subList);
}
}
// Also test the whole list:
subLists.add(input);
Then you need to check for each element if the list is a palindrome. To test if a list is a palindrome, element n must be compared to element listSize - 1 - n.
We only need to check half of the elements.
static boolean isPalindrome(List<Integer> subList) {
for (int i = 0; i < subList.size() / 2; i++) {
if (!Objects.equals(subList.get(i), subList.get(subList.size() - 1 - i))) {
return false;
}
}
return true;
}
If you want to remove duplicates, then you can put the elements into a Set.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
I'm trying to implement a code to mergeSort a list. I was told to use getFirst() and addAll() but I don't know how so this is what I came up with:
Also, I can't use a void for mergeSort method - because it gives me an error so I have to return something.
Any suggestions will be very appreciate
You were close in terms of getting your approach working. Here's what I came up with. I changed the logic to create new lists to contain the results at each level rather than creating new lists when you break up a list. There's no reason to copy a list just to keep it in the same order. It's the list in the new order that needs to be fresh storage. This limits the number of copies and I think makes the logic easier, since you can then just append onto the end of each result list instead of having to have logic to set the right position in each list.
import java.util.Arrays;
import java.util.LinkedList;
class Test {
public static LinkedList<Integer> mergeSort(LinkedList<Integer> list, int start, int count) {
if (count < 2) {
LinkedList<Integer> result = new LinkedList<>();
result.add(list.get(start));
return result;
}
int size1 = count / 2;
int size2 = count - size1;
LinkedList<Integer> list_1 = mergeSort(list, start, size1);
LinkedList<Integer> list_2 = mergeSort(list, start + size1, size2);
return merge(list_1, list_2);
}
public static LinkedList<Integer> merge(LinkedList<Integer> list_1, LinkedList<Integer> list_2) {
LinkedList<Integer> result = new LinkedList<>();
int i = 0, j = 0;
while (i < list_1.size() && j < list_2.size())
if (list_1.get(i) < list_2.get(j))
result.add(list_1.get(i++));
else
result.add(list_2.get(j++));
while (i < list_1.size())
result.add(list_1.get(i++));
while (j < list_2.size())
result.add(list_2.get(j++));
return result;
}
public static LinkedList<Integer> mergeSort(LinkedList<Integer> list) {
return mergeSort(list, 0, list.size());
}
public static void main(String[] args) {
LinkedList<Integer> myList = new LinkedList<>(Arrays.asList(22, 45, 1234, 77, 4, 1111, 999, 12, 88, 44, 7777, 22, 33, 44, 666));
LinkedList<Integer> sorted = mergeSort(myList);
System.out.println(sorted);
}
}
Result:
[4, 12, 22, 22, 33, 44, 44, 45, 77, 88, 666, 999, 1111, 1234, 7777]
UPDATE: As #rcgldr points out, the above solution doesn't meet the stated guidelines. With this new version of the merge() method, the guidelines are met. Note that this doesn't make the algorithm much more efficient (both versions create lots of lists and are quite INEFFICIENT), but it does save a bunch of walking to random positions in lists in favor of working from the front each list:
public static LinkedList<Integer> merge(LinkedList<Integer> list_1, LinkedList<Integer> list_2) {
LinkedList<Integer> result = new LinkedList<>();
while (list_1.size() > 0 && list_2.size() > 0)
if (list_1.peek() < list_2.peek()) {
result.add(list_1.getFirst());
list_1.remove();
}
else {
result.add(list_2.getFirst());
list_2.remove();
}
result.addAll(list_1);
result.addAll(list_2);
return result;
}
Also note that calling getFirst() and then remove() is redundant. remmove() can be called by itself to both grab the next element and remove it from the list. I went with calling both methods to meet the requirements.
Although there is an accepted answer, this code uses getFirst() and addAll() as mentioned in the original post, in what I assume is the intended purpose of this exercise. It uses a bottom up merge sort, which avoids the top down issue having to scan lists to find mid points to split lists.
The basic concept of a bottom up merge sort is two 1 node lists are merged to create a sorted 2 node list, two sorted 2 node lists are merged to create a sorted 4 node list, two sorted 4 node lists are merged to create a sorted 8 node list, and so on, similar to bottom up merge sort for arrays.
A bottom up merge sort uses a small array of lists (java), pointers to nodes (C), or iterators to nodes (C++) to hold the lists. For this example code, I use an array of 32 lists. For each member of array, the member is either null or refers to a list where the number of nodes for the list for array[i] has 2^i nodes: array[0] 1 node, array[1] 2 nodes, array[2] 4 nodes, ..., array[30] 2^30 = 1 billion nodes. The last member will have 2^31 = 2 billion nodes or more if the total number of nodes is >= 2^32 (4 billion) nodes.
Nodes are removed from the front of the source list one at a time and each node from the source list is used to create a list mm with 1 node, then mm is merged into the array, stopping at the first null encountered:
while(source_list not empty){
mm = source_list.removeNode(() // mm = next node from source list
for(i = 0; i < 32; i++){ // merge mm into array
if(array[i] != null){
mm = merge(array[i], mm)
array[i] = null
} else {
break
}
}
if(i == 32) // if i == 32, there is no array[32], so
i = 31 // use array[31] instead
array[i] = mm
}
The pattern looks like this:
mm = next node from source list // 1 node list
array[0] = mm
mm = next node from source list // 1 node list
mm = merge(array[0], mm) // 2 node list
array[0] = null
array[1] = mm
mm = next node from source list // 1 node list
array[0] = mm
mm = next node from source list // 1 node list
mm = merge(array[0], mm) // 2 node list
array[0] = null
mm = merge(array[1], mm) // 4 node list
array[1] = null
array[2] = mm
Once the last node is merged into the array, all the lists in the array are merged to form a single sorted list:
mm = empty list
for(i = 0; i < 32; i++){
if(all[i] != null){
mm = merge(all[i], mm);
all[i] = null; // (for garbage collection)
}
}
Java's native double linked list class doesn't provide a means to move nodes within a list or between lists, so a node has to be deleted when an element is retrieved from the front of a list, and a node has to be created when an element is appended to the back of a list, an extra overhead. In the case of C++ standard double link list class std::list, std::list::splice() can be used to move nodes within a list or between lists, reducing the overhead, such as dst.splice(dst.end(), src, src.begin()), which moves the beginning node (first node) from src to the ending node (last node) of dst.
public static LinkedList<Integer> merge(LinkedList<Integer> ll,
LinkedList<Integer> rr)
{
if(ll.isEmpty())
return rr;
if(rr.isEmpty())
return ll;
LinkedList<Integer> mm = new LinkedList<>();
while(true){
if(ll.getFirst().compareTo(rr.getFirst()) <= 0){
mm.add(ll.removeFirst());
if(!ll.isEmpty())
continue;
mm.addAll(rr);
break;
} else {
mm.add(rr.removeFirst());
if(!rr.isEmpty())
continue;
mm.addAll(ll);
break;
}
}
return mm;
}
public static LinkedList<Integer> mergeSort(LinkedList<Integer> ll)
{
if(ll == null || ll.size() < 2)
return ll;
int i;
final int ASZ = 32; // create array (of nulls)
LinkedList<Integer>[] all= new LinkedList[ASZ];
// merge nodes into array
LinkedList<Integer> mm;
do{
mm = new LinkedList<>(); // mm = next node
mm.add(ll.removeFirst());
// merge mm into array
for(i = 0; (i < ASZ) && (all[i] != null); i++){
mm = merge(all[i], mm);
all[i] = null;
}
if(i == ASZ) // don't go past end of array
i--;
all[i] = mm;
}while(!ll.isEmpty());
// merge array into single list
mm = new LinkedList<>();
for(i = 0; i < ASZ; i++){
if(all[i] != null){
mm = merge(all[i], mm);
all[i] = null;
}
}
return (mm);
}
test code, takes about 3 to 5 seconds to sort 4 million (2^22) nodes.
public static void main(String[] args) {
final int COUNT = 4*1024*1024;
LinkedList<Integer> ll = new LinkedList<>();
Random r = new Random();
for(int i = 0; i < COUNT; i++)
ll.addLast(r.nextInt());
long bgn, end;
bgn = System.currentTimeMillis();
ll = mergeSort(ll);
end = System.currentTimeMillis();
System.out.println("milliseconds " + (end-bgn));
// check sort
int i;
i = ll.removeFirst();
int j = i;
while(!ll.isEmpty()){
j = ll.removeFirst();
if(i > j)
break;
i = j;
}
if(i == j)
System.out.println("passed");
else
System.out.println("failed");
}
In the merge code, I'm getting faster and more consistent run times using a loop instead of addAll, from 3 to 5 seconds down to 2.8 to 3.2 seconds, but the assignment states to use addAll.
// mm.addAll(rr); // replace with loop is faster
do
mm.add(rr.removeFirst());
while(!rr.isEmpty());
This question already has an answer here:
What is a StringIndexOutOfBoundsException? How can I fix it?
(1 answer)
Closed 2 years ago.
I have a homework ,and which I have tried to solve for serveral hours.
Description:we have 2 sorted array list ,the length of list a is n, the length of list b is m;we assume a and b are already sorted, and that the lists a and b do not contain duplicates.And I thought as following ,but I always get a error:index out of bound .
public static List<Integer> union(List<Integer> list1, List<Integer> list2 ) {
List<Integer> union = new ArrayList<>();
int n1 = 0;
int n2 = 0;
for (int i = 0; i < list1+list2; i++) {
....
}
If you loop till m + n one of the array which might have a lesser size than the other will get exhausted and hence the array index out of bound. Instead you can use a while loop which will loop only till the minimum size of the two lists and later, since the list is sorted you can add all the remaining elements from the bigger of the two lists into the union
// iterate till the minimum of two lists
while(index1 < a.size() && index2 < b.size()) {
if (a.get( index1 ) > b.get( index2 )) {
union.add(i++, b.get( index2++ ));
}
else {
union.add(i++, a.get( index1++ ));
}
}
//add the elements of the bigger list to the union
while(index1 < a.size()) {
union.add(i++ ,a.get( index1++ ));
}
while(index2 < b.size()){
union.add(i++, b.get( index2++ ));
}
public static List<Integer> union(List<Integer> one, List<Integer> two) {
List<Integer> res = new ArrayList<>(one.size() + two.size());
for (int i = 0, a1 = 0, a2 = 0; i < one.size() + two.size(); i++) {
if (a1 == one.size())
res.add(two.get(a2++));
else if (a2 == two.size())
res.add(one.get(a1++));
else
res.add(one.get(a1) <= two.get(a2) ? one.get(a1++) : two.get(a2++));
}
return res;
}
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Consider input [3,2,4] and target is 6. I added (3,0) and (2,1) to the map and when I come to 4 and calculate value as 6 - 4 as 2 and when I check if 2 is a key present in map or not, it does not go in if loop.
I should get output as [1,2] which are the indices for 2 and 4 respectively
public int[] twoSum(int[] nums, int target) {
int len = nums.length;
int[] arr = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0;i < len; i++)
{
int value = nums[i] - target;
if(map.containsKey(value))
{
System.out.println("Hello");
arr[0] = value;
arr[1] = map.get(value);
return arr;
}
else
{
map.put(nums[i],i);
}
}
return null;
}
I don't get where the problem is, please help me out
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice. Consider input [3,2,4] and target is 6. I added (3,0) and (2,1) to the map and when I come to 4 and calculate value as 6 - 4 as 2 and when I check if 2 is a key present in map or not, it does not go in if loop.
Okay, let's take a step back for a second.
You have a list of values, [3,2,4]. You need to know which two will add up 6, well, by looking at it we know that the answer should be [1,2] (values 2 and 4)
The question now is, how do you do that programmatically
The solution is (to be honest), very simple, you need two loops, this allows you to compare each element in the list with every other element in the list
for (int outter = 0; outter < values.length; outter++) {
int outterValue = values[outter];
for (int inner = 0; inner < values.length; inner++) {
if (inner != outter) { // Don't want to compare the same index
int innerValue = values[inner];
if (innerValue + outterValue == targetValue) {
// The outter and inner indices now form the answer
}
}
}
}
While not highly efficient (yes, it would be easy to optimise the inner loop, but given the OP's current attempt, I forewent it), this is VERY simple example of how you might achieve what is actually a very common problem
int value = nums[i] - target;
Your subtraction is backwards, as nums[i] is probably smaller than target. So value is getting set to a negative number. The following would be better:
int value = target - nums[i];
(Fixing this won't fix your whole program, but it explains why you're getting the behavior that you are.)
This code for twoSum might help you. For the inputs of integer array, it will return the indices of the array if the sum of the values = target.
public static int[] twoSum(int[] nums, int target) {
int[] indices = new int[2];
outerloop:
for(int i = 0; i < nums.length; i++){
for(int j = 0; j < nums.length; j++){
if((nums[i]+nums[j]) == target){
indices[0] = i;
indices[1] = j;
break outerloop;
}
}
}
return indices;
}
You can call the function using
int[] num = {1,2,3};
int[] out = twoSum(num,4);
System.out.println(out[0]);
System.out.println(out[1]);
Output:
0
2
You should update the way you compute for the value as follows:
int value = target - nums[i];
You can also check this video if you want to better visualize it. It includes Brute force and Linear approach:
I found this code online and it works well to permute through the given array and return all possible combinations of the numbers given. Does anyone know how to change this code to incorporate a 2D array instead?
public static ArrayList<ArrayList<Integer>> permute(int[] numbers) {
ArrayList<ArrayList<Integer>> permutations = new ArrayList<ArrayList<Integer>>();
permutations.add(new ArrayList<Integer>());
for ( int i = 0; i < numbers.length; i++ ) {
ArrayList<ArrayList<Integer>> current = new ArrayList<ArrayList<Integer>>();
for ( ArrayList<Integer> p : permutations ) {
for ( int j = 0, n = p.size() + 1; j < n; j++ ) {
ArrayList<Integer> temp = new ArrayList<Integer>(p);
temp.add(j, numbers[i]);
current.add(temp);
}
}
permutations = new ArrayList<ArrayList<Integer>>(current);
}
return permutations;
}
This is what I have attempted:
public static int[][] permute(int[] numbers){
int[][] permutations = new int[24][4];
permutations[0] = new int[4];
for ( int i = 0; i < numbers.length; i++ ) {
int[][] current = new int[24][4];
for ( int[] permutation : permutations ) {
for ( int j = 0; j < permutation.length; j++ ) {
permutation[j] = numbers[i];
int[] temp = new int[4];
current[i] = temp;
}
}
permutations = current;
}
return permutations;
}
However this returns all zeroes. I chose 24 and 4 because that is the size of the 2D array that I need.
Thanks
It’s not really that easy. The original code exploits the more dynamic behaviour of ArrayList, so a bit of hand coding will be necessary. There are many correct thoughts in your code. I tried to write an explanation of the issues I saw, but it became too long, so I decided to modify your code instead.
The original temp.add(j, numbers[i]); is the hardest part to do with arrays since it invloves pushing the elements to the right of position j one position to the right. In my version I create a temp array just once in the middle loop and shuffle one element at a time in the innermost loop.
public static int[][] permute(int[] numbers) {
// Follow the original here and create an array of just 1 array of length 0
int[][] permutations = new int[1][0];
for (int i = 0; i < numbers.length; i++) {
// insert numbers[i] into each possible position in each array already in permutations.
// create array with enough room: when before we had permutations.length arrays, we will now need:
int[][] current = new int[(permutations[0].length + 1) * permutations.length][];
int count = 0; // number of new permutations in current
for (int[] permutation : permutations) {
// insert numbers[i] into each of the permutation.length + 1 possible positions of permutation.
// to avoid too much shuffling, create a temp array
// and use it for all new permutations made from permutation.
int[] temp = Arrays.copyOf(permutation, permutation.length + 1);
for (int j = permutation.length; j > 0; j--) {
temp[j] = numbers[i];
// remember to make a copy of the temp array
current[count] = temp.clone();
count++;
// move element to make room for numbers[i] at next position to the left
temp[j] = temp[j - 1];
}
temp[0] = numbers[i];
current[count] = temp.clone();
count++;
}
assert count == current.length : "" + count + " != " + current.length;
permutations = current;
}
return permutations;
}
My trick with the temp array means I don’t get the permutations in the same order as in the origianl code. If this is a requirement, you may copy permutation into temp starting at index 1 and shuffle the opposite way in the loop. System.arraycopy() may do the initial copying.
The problem here is that you really need to implement properly the array version of the ArrayList.add(int,value) command. Which is to say you do an System.arraycopy() and push all the values after j, down one and then insert the value at j. You currently set the value. But, that overwrites the value of permutation[j], which should actually have been moved to permutations[j+1] already.
So where you do:
permutation[j] = numbers[i];
It should be:
System.arraycopy(permutation,j, permutations, j+1, permutations.length -j);
permutation[j] = numbers[i];
As the ArrayList.add(int,value) does that. You basically wrongly implemented it as .set().
Though personally I would scrap the code and go with something to dynamically make those values on the fly. A few more values and you're talking something prohibitive with regard to memory. It isn't hard to find the nth index of a permutation. Even without allocating any memory at all. (though you need a copy of the array if you're going to fiddle with such things without incurring oddities).
public static int[] permute(int[] values, long index) {
int[] returnvalues = Arrays.copyOf(values,values.length);
if (permutation(returnvalues, index)) return returnvalues;
else return null;
}
public static boolean permutation(int[] values, long index) {
return permutation(values, values.length, index);
}
private static boolean permutation(int[] values, int n, long index) {
if ((index == 0) || (n == 0)) return (index == 0);
int v = n-(int)(index % n);
int temp = values[n];
values[n] = values[v];
values[v] = temp;
return permutation(values,n-1,index/n);
}
I was trying to solve following programming exercise from some java programming book
Write method that partitions the array using the first element, called a pivot. After the partition, the elements in the list are rearranged so that all the elements before the pivot are less than or equal to the pivot and the elements after the pivot are greater than the pivot. The method returns the index where the pivot is located in the new list. For example, suppose the list is {5, 2, 9, 3, 6, 8}. After the partition, the list becomes {3, 2, 5, 9, 6, 8}. Implement the method in a way that takes at most array.length comparisons.
I've implemented solution, but it takes much more than array.length comparisons.
The book itself has solution, but unfortunately it's just plain wrong (not working with some inputs). I've seen the answer to this similar question, and understood "conquer" part of Quicksort algorithm, but in this algorithm values are partitioned using mid-value, but in my case using of 1st array value as a pivot is required.
This is the pivot routine from the linked answer (adapted from source here).
int split(int a[], int lo, int hi) {
// pivot element x starting at lo; better strategies exist
int x=a[lo];
// partition
int i=lo, j=hi;
while (i<=j) {
while (a[i]<x) i++;
while (a[j]>x) j--;
if (i<=j) swap(a[i++], a[j--]);
}
// return new position of pivot
return i;
}
The number of inter-element comparisons in this algorithm is either n or n+1; because in each main loop iteration, i and j move closer together by at exactly c units, where c is the number of comparisons performed in each of the inner while loops. Look at those inner loops - when they return true, i and j move closer by 1 unit. And if they return false, then, at the end of the main loop, i and j will move closer by 2 units because of the swap.
This split() is readable and short, but it also has a very bad worst-case (namely, the pivot ending at either end; follow the first link to see it worked out). This will happen if the array is already sorted either forwards or backwards, which is actually very frequent. That is why other pivot positions are better: if you choose x=a[lo+hi/2], worst-case will be less common. Even better is to do like Java, and spend some time looking for a good pivot to steer clear from the worst case. If you follow the Java link, you will see a much more sophisticated pivot routine that avoids doing extra work when there are many duplicate elements.
It seem that the algorithm (as taken from "Introduction to algorihtm 3rd ed") can be implemented as follows (C++) should be similar in Java`s generics:
template <typename T> void swap_in_place(T* arr, int a, int b)
{
T tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
template <typename T> int partition(T* arr, int l, int r)
{
T pivot = arr[r];
int i = l-1;
int j;
for(j=l; j < r; j++) {
if (arr[j] < pivot /* or cmp callback */) {
// preincrement is needed to move the element
swap_in_place<T>(arr, ++i, j);
}
}
// reposition the pivot
swap_in_place(arr, ++i, j);
return i;
}
template <typename T> void qsort(T* arr, int l, int r)
{
if ( l < r ) {
T x = partition<T>(arr, l, r);
qsort(arr, l, x-1);
qsort(arr, x+1, r);
}
}
However, its a simple pseudocode implementation, I dont know if it`s the best pivot to pick from. Maybe (l+r)/2 would be more proper.
Pretty simple solution with deque:
int [] arr = {3, 2, 5, 9, 6, 8};
Deque<Integer> q = new LinkedBlockingDeque<Integer>();
for (int t = 0; t < arr.length; t++) {
if (t == 0) {
q.add(arr[t]);
continue;
}
if (arr[t] <= arr[0])
q.addFirst(arr[t]);
else
q.addLast(arr[t]);
}
for (int t:q) {
System.out.println(t);
}
Output is:
2
3
5 <-- pivot
9
6
8
There is video that I made on Pivot based partition I explained both the methods of patitioning.
https://www.youtube.com/watch?v=356Bffvh1dA
And based on your(the other) approach
https://www.youtube.com/watch?v=Hs29iYlY6Q4
And for the code. This is a code I wrote for pivot being the first element and it takes O(n) Comparisons.
void quicksort(int a[],int l,int n)
{
int j,temp;
if(l+1 < n)
{
int p=l;
j=l+1;
for(int i=l+1;i<n;++i)
{
if(a[i]<a[p])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
j++;
}
}
temp=a[j-1];
a[j-1]=a[p];
a[p]=temp;
quicksort(a,l,j);
quicksort(a,j,n);
}
}
The partition function below works as follow:
The last variable points to the last element in the array that has not been compared to the pivot element and can be swapped.
If the element directly next to the pivot element is less than the pivot
element. They are swapped.
Else if the pivot element is less than the next element, the nextelement is swapped with the element whose index is the last variable.
static int partition(int[] a){
int pivot = a[0];
int temp, index = 0;
int last = a.length -1;
for(int i = 1; i < a.length; i++){
//If pivot > current element, swap elements
if( a[i] <= pivot){
temp = a[i];
a[i] = pivot;
a[i-1] = temp;
index = i;
}
//If pivot < current elmt, swap current elmt and last > index of pivot
else if( a[i] > pivot && last > i){
temp = a[i];
a[i] = a[last];
a[last] = temp;
last -= 1;
i--;
}
else
break;
}
return index;
}