Sort DSA

Sort:

1. Bubble

import java.util.Arrays; // Import Arrays class
//Bubble Sort
class BubbleSort {
    public static void bubbleSort(int array[]) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                }
            }
        }
    }

    public static void main(String args[]) {
        int array[] = {1, 7, 5, 6};
        bubbleSort(array); // Sort the array
        System.out.println("Sorted Array:");
        System.out.println(Arrays.toString(array)); // Print the sorted array
    }
}

2. Selection

import java.util.Arrays; // Import Arrays class
//Selection Sort
class ArrayDemo {
    public static void selectionSort(int array[]) {
        int n = array.length;
        for (int i = 0; i < n - 1; i++) {
            int min_idx = i;
            for (int j = i + 1; j < n; j++) {
                if (array[j] < array[min_idx]) {
                    min_idx = j;
                }
            }
            int tmp = array[min_idx];
            array[min_idx] = array[i];
            array[i] = tmp;
        }
    }

    public static void main(String args[]) {
        int array[] = {1, 7, 5, 6, 2, -1};
        selectionSort(array); // Sort the array
        System.out.println("Sorted Array:");
        System.out.println(Arrays.toString(array)); // Print the sorted array
    }
}

3. Selection

import java.util.Array
//Insert Sort
class ArrayDemo {
    public static void insertSort(int array[]) {
        int n = array.length;
        for(int i = 1; i<n;i++){
         int key = array[i];
         int j=i-1;

         //compare KEY with each element on the left of it until an element smaller than it is found
         //for descending order, change key<array[j] to key>array[j];
         while(j>=0 && key < array[j]){
            array[j+1] = array[j];
            --j;
         }
         array[j+1]=key;
        }
    }
//Driver code
    public static void main(String args[]) {
        int array[] = {1, 7, 5, 6, 2, -7, 3};
        insertSort(array); // Sort the array
        System.out.println("Sorted Array:");
        System.out.println(Arrays.toString(array)); // Print the sorted array
    }
}

- Divide and Conquer Algorithm

4. Quick: (1)Single (2)Qual 

5. Merge

C++ 是一种非常适合实现数据结构算法的编程语言。以下是一个简单的示例,展示了如何使用 C++ 实现数据结构算法。 1. 实现一个链表 ```cpp #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; int main() { ListNode *head = new ListNode(1); ListNode *p = head; p->next = new ListNode(2); p = p->next; p->next = new ListNode(3); p = p->next; p->next = new ListNode(4); p = p->next; p->next = new ListNode(5); p = head; while (p != NULL) { cout << p->val << " "; p = p->next; } cout << endl; return 0; } ``` 2. 实现一个栈 ```cpp #include <iostream> #include <stack> using namespace std; int main() { stack<int> st; st.push(1); st.push(2); st.push(3); st.push(4); st.push(5); while (!st.empty()) { cout << st.top() << " "; st.pop(); } cout << endl; return 0; } ``` 3. 实现一个队列 ```cpp #include <iostream> #include <queue> using namespace std; int main() { queue<int> q; q.push(1); q.push(2); q.push(3); q.push(4); q.push(5); while (!q.empty()) { cout << q.front() << " "; q.pop(); } cout << endl; return 0; } ``` 4. 实现一个排序算法 ```cpp #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> nums = {5, 2, 3, 4, 1}; sort(nums.begin(), nums.end()); for (auto num : nums) { cout << num << " "; } cout << endl; return 0; } ``` 这只是一些简单的示例,C++ 中有许多更复杂的数据结构算法可以实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值