#Sorting & Searching

1 messages ยท Page 1 of 1 (latest)

slow furnaceBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question ๐Ÿค–. Maybe it helps! In any case, a human is on the way ๐Ÿ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

Sure! I'll explain the selection sort, bubble sort, linear search, and binary search algorithms in Java and provide you with their syntax.

  1. Selection Sort:
    Selection sort is a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. Here's the syntax:
#
public class SelectionSort {
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        
        for (int i = 0; i < n-1; i++) {
            int minIndex = i;
            
            for (int j = i+1; j < n; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            
            int temp = arr[minIndex];
            arr[minIndex] = arr[i];
            arr[i] = temp;
        }
    }
    
    public static void main(String[] args) {
        int[] arr = {64, 25, 12, 22, 11};
        selectionSort(arr);
        
        System.out.println("Sorted array:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
#
  1. Bubble Sort:
    Bubble sort is another simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. Here's the syntax:
#
public class BubbleSort {
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        
        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};
        bubbleSort(arr);
        
        System.out.println("Sorted array:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}
#
  1. Linear Search:
    Linear search is a simple searching algorithm that sequentially checks each element of the array until it finds the target value. Here's the syntax:
#
public class LinearSearch {
    public static int linearSearch(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i;
            }
        }
        
        return -1; // Target not found
    }
    
    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11};
        int target = 22;
        
        int index = linearSearch(arr, target);
        
        if (index != -1) {
            System.out.println("Element found at index " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}
#
  1. Binary Search:
    Binary search is a more efficient searching algorithm that works on sorted arrays by repeatedly dividing the search interval in half. Here's the syntax:
#
public class BinarySearch {
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 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};
        int target = 22;
        
        int index = binarySearch(arr, target);
        
        if (index != -1) {
            System.out.println("Element found at index " + index);
        } else {
            System.out.println("Element not found");
        }
    }
}
#

I hope this helps! Let me know if you have any further questions.

desert sequoia
#

Help me with an example of all these

#

Any mod or experts pls

sturdy cradle
#

all helpers got pinged

#

please have some patience, thanks

copper tendon
#

nice question

#

but it's better for you and us to pick one from those

#

as details may take some times

#

also, to avoid conflicts

#

what would you say deal ?

opaque prairie
#

Also: what's unclear about the provided example by chatgpt?

fringe mango
#

help me by giving syntax of all the above mentioned sortings & searches. We are not doing ur homework, ask about whats unclear to you and we will help you solve it

desert sequoia
#

I can't use the -1 in my examination

desert sequoia
opaque prairie
#

but chatgpt provided specific functions?

desert sequoia
#

Dude in those gpt used it as a method then it executed by creating a main method in my case I have to execute all things in the public static void main method only

#

GPT also used return function which is not in my current year syllabus

#

Ik we can use this method but I can't write this above chat gpt syntax in my examination

opaque prairie
#

so rather than having the code snippet in a standalone function just put the specific function in your main?

desert sequoia
#

Can we do binary search using for loop?

desert sequoia
#

Gm helpers & staff team

rapid ocean
desert sequoia
#

Can u type the code

opaque prairie
#

cfr This is not a code-writing service, we do not do your homework for you - instead, we will help you solve it yourself Where are you stuck with adapting the GPT example to use a for-loop

#

If this is for an exam you really should try to understand the samples and what's going on, rather than asking us to write it for you and memorizing it.

desert sequoia
#

If u don't want to help me than go

keen briar
#

What do you have so far? Binary search is a matter of splitting a range by a 'pivot' value, if that is not the desired value, working out whether to repeat the exercise on the left or right part of the split. That can be done recursively or iteratively.

timber marsh
#

@desert sequoia we won't spoonfeed your homework

desert sequoia
#

So I am not giving u a full program to write

#

If you all don't want to help me don't do

#

I will do it

restive niche
#

so what is your question?

desert sequoia
#

No please

#

Now I don't want to talk with any of the staff team and experts

restive niche
#

so you dont want help at all?

desert sequoia
#

Yea

restive niche
#

what is the reason for this thread then?

desert sequoia
#

I thought you all would help me

restive niche
#

I am trying and you are saying that you dont want to talk to me lol

desert sequoia
#

How u will help me

civic moss
# desert sequoia How u will help me

most of the time, it's just answering questions or providing you in the right direction but it's going to be up to you to write the code. if you make any mistakes, we'll explain how to fix it without providing the exact solution. reason for this is so you're learning as part of the process and not being handed the solution

#

the best way to get a code example is to ask for it in psudo

desert sequoia
restive niche
#

But I am asking for the code so that I can complete my program
its not about the size you are asking, you are asking others to code smth for you

desert sequoia
#

When did I ask abt size

restive niche
#

you didnt

#

but you asked for code

#

and we are not a code writing service

desert sequoia
#

Ik

#

Are you a employee from Java

restive niche
#

no

desert sequoia
#

Ok

restive niche
#

why are you asking?

desert sequoia
#

Just for info.

restive niche
#

but you understand what I mean right? and what wazei is saying right?

opaque prairie
desert sequoia
#

It's a great pleasure to see you

restive niche
#

why are you asking?

#

and no im not

desert sequoia
#

I am not gonna attack you

#

So don't worry

desert sequoia
#

Now what happened?

restive niche
#

what?

desert sequoia
#

Nothing

#

U are always free ig

civic moss
#

so just don't expect us to write any code but we'll tell you where you are wrong ๐Ÿ‘

desert sequoia
#

Ok

#

Don't give me code

#

Just tell me yes or no

Can we use for loop for binary search

keen briar
#

Yes. I answered that previously

#

I probably wouldn't use for - while is a better fit for the iterative implementation. But a for can act just like while

desert sequoia
#

Yeah

#

Idk why my examination is against while loop in binary search

slow furnaceBOT
#

@desert sequoia

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘