Need help converting this C++ code to java. 1. Bubble Sort #include using namespace std; void swap(int * xp, int * yp) { int temp = * xp; * xp = * yp * yp - temp; } void bubbleSort(int arr[], int n) { int i, j; for (i = 0; i < n - 1; i++) for (j - 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) swap( & arr[j], & arr[j + 1]); } void printArray(int arr[], int size) { int i; for (i = 0; i < size; i++) cout << arr[i] << " "; cout << end1; } //Driver ClassLoader int main() { int arr[] = {1, 2, 3, 7, 5, 6, 4}; int n = sizeof(arr)/sizeof(arr[0]); bubbleSort(arr, n); cout <<"Sorted Array: \n"; printArray(arr, n); return 0; } 2. Inserted Sort #include using namespace std; void insertionSort(int arr[], int n) { int i, key, j; { key = arr[i]; j = i-1; while (j >= 0 && arr[j]>key) { arr[j+1]=arr[j]; j=j-1; } arr[j+1]=key; } } void printArray(int arr[], int n) { int i; for (i=0; i
Need help converting this C++ code to java.
1. Bubble Sort
#include <bits/stdc++.h>
using namespace std;
void swap(int * xp, int * yp) {
int temp = * xp;
* xp = * yp *
yp - temp;
}
void bubbleSort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++)
for (j - 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap( & arr[j], & arr[j + 1]);
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << end1;
}
//Driver ClassLoader
int main()
{
int arr[] = {1, 2, 3, 7, 5, 6, 4};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
cout <<"Sorted Array: \n";
printArray(arr, n);
return 0;
}
2. Inserted Sort
#include <bits/stdc++.h>
using namespace std;
void insertionSort(int arr[], int n)
{
int i, key, j;
{
key = arr[i];
j = i-1;
while (j >= 0 && arr[j]>key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i=0; i<n; i++)
cout << arr[i] <<" ";
cout << end1;
}
//Driver code//
int main()
{
int arr[] = {7, 2, 4, 3, 1};
int n = sizeof(arr)/sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)