0% found this document useful (0 votes)
2 views5 pages

C++ and Java Sorting & Searching Algorithms

The document contains implementations of sorting and searching algorithms in both C++ and Java. It includes a bubble sort algorithm, a binary search algorithm, and a sequential search algorithm, demonstrating how to sort an array and find an element within it. Each algorithm is provided with a main function to test its functionality with a sample array and target value.

Uploaded by

jemmagtuba143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

C++ and Java Sorting & Searching Algorithms

The document contains implementations of sorting and searching algorithms in both C++ and Java. It includes a bubble sort algorithm, a binary search algorithm, and a sequential search algorithm, demonstrating how to sort an array and find an element within it. Each algorithm is provided with a main function to test its functionality with a sample array and target value.

Uploaded by

jemmagtuba143
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <iostream>

Using namespace std;

Void bubbleSort(int arr[], int size) {

For (int I = 0; I < size – 1; ++i) {

For (int j = 0; j < size – I – 1; ++j) {

If (arr[j] > arr[j + 1]) {

Swap(arr[j], arr[j + 1]);

Int main() {

Int arr[] = {64, 34, 25, 12, 22, 11, 90};

Int size = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, size);

cout << “Sorted array: “;

for (int I = 0; I < size; ++i) {

cout << arr[i] << “ “;

Return 0;

public class BubbleSort {

public static void bubbleSort(int arr[]) {

int n = [Link];
for (int i = 0; i < n - 1; ++i) {

for (int j = 0; j < n - i - 1; ++j) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

public static void main(String args[]) {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = [Link];

bubbleSort(arr);

[Link]("Sorted array: ");

for (int i = 0; i < n; ++i) {

[Link](arr[i] + " ");

#include <iostream>

Using namespace std;

Int binarySearch(int arr[], int size, int target) {

Int left = 0, right = size – 1;

While (left <= right) {


Int mid = left + (right – left) / 2;

If (arr[mid] == target) {

Return mid;

If (arr[mid] < target) {

Left = mid + 1;

} else {

Right = mid – 1;

Return -1; // Target not found

Int main() {

Int arr[] = {11, 12, 22, 25, 34, 64, 90};

Int size = sizeof(arr) / sizeof(arr[0]);

Int target = 34;

Int index = binarySearch(arr, size, target);

If (index != -1) {

Cout << “Element found at index “ << index;

} else {

Cout << “Element not found”;

Return 0;

public class BinarySearch {


public static int binarySearch(int arr[], int target) {

int left = 0, right = [Link] - 1;

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == target) {

return mid;

if (arr[mid] < target) {

left = mid + 1;

} else {

right = mid - 1;

return -1; // Target not found

public static void main(String args[]) {

int arr[] = {11, 12, 22, 25, 34, 64, 90};

int target = 34;

int index = binarySearch(arr, target);

if (index != -1) {

[Link]("Element found at index " + index);

} else {

[Link]("Element not found");

}
Public class SequentialSearch {

Public static int sequentialSearch(int arr[], int target) {

For (int I = 0; I < [Link]; ++i) {

If (arr[i] == target) {

Return I;

Return -1; // Target not found

Public static void main(String args[]) {

Int arr[] = {11, 12, 22, 25, 34, 64, 90};

Int target = 34;

Int index = sequentialSearch(arr, target);

If (index != -1) {

[Link](“Element found at index “ + index);

} else {

[Link](“Element not found”);

You might also like